php中查询最近一个月的查询代码报错,请教咋办
时间:2021-07-01 10:21:17
帮助过:3人阅读
php中查询最近一个月的查询代码报错,请问咋办
//最近一月的订单
$sql_month="select order_id , order_number from order_table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(order_date)" ;
var_dump($sql_month) ;
$mysql_month=mysql_query($sql_month) or die("Could not query.".mysql_error()) ;
$info_month=mysql_num_rows($mysql_month) ;
if($info_month==""){
$html_info_month="最近一月没有订单。" ;
}
else{
$str=mysql_fetch_array($mysql_month) ;
// $html_info_month="最近一月的订单" ;
$html_info_month="
" ;
do{
$order_id=iconv("gb2312" , "utf-8//IGNORE" , $str["order_id"]) ;
$order_number=iconv("gb2312" , "utf-8//IGNORE" , $str["order_number"]) ;
$html_info_month.="- ".$order_number."
" ;
}while($str=mysql_fetch_array($mysql_month)) ;
$html_info_month.="
" ;
}
查询最近一个月的select语句,是正确的吗?
报错:
string(120) "select order_id , order_number from order_table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(order_date)" Could not query.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') <= date(order_date)' at line 1
------解决方案--------------------select order_id , order_number from order_table where date > ".strtotime('-1 month')
------解决方案--------------------1 其实你只想问那句sql,那就不要贴代码了,太多了,好多人不喜欢看
2 $sql_month="select order_id , order_number from order_table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(order_date)" 意思是找出那些订单的日期加上一个月都还要小于等于当前日期的,而最近一个月的意思是订单日期比当前日期小一个月之内的数据,也就是订单日期加上一个月大于等于当前日期的,所以应该是select order_id , order_number from order_table where DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) >= date(order_date)"