如下语句报错,cash_date是数据库中需要查询的字段名称,请懂的解答下,谢谢
注意:本问题要查询的数据表中cash_date
字段值的类型是“2021-06-11 20:10:09”
PHP中的需拼接的部分查询条件语句是(pdo查询语句):
if ($keyword['timebucket']) {
$today = date("YYYY-mm-dd");;
switch($keyword['timebucket']) {
case 'today': $condition .= " AND datediff(cash_date,date('YYYY-mm-dd')) = 0 "; break;//今天
case 'yesterday': $condition .= " AND datediff(cash_date,$today)=1 "; break;//昨天
case 'week': $condition .= " AND datediff(cash_date,$today)<=7 "; break;//本周
case 'las_week': $condition .= " AND datediff(cash_date,$today)>7 "; break;//7天前
case '30today': $condition .= " AND datediff(cash_date,$today)<=30 "; break;//30天内
case 'month': $condition .= " AND datediff(cash_date,$today)=0 "; break;//本月
case 'last_month': $condition .= " AND datediff(cash_date,$today)=1 "; break;//上月
case 'year': $condition .= " AND datediff(cash_date,$today)=0 "; break;//今年
case 'last year': $condition .= " AND datediff(cash_date,$today)=1 "; break;//去年
}
}
完整sql语句及报错如下:
Warning: SQL:
SELECT * FROM `数据表名称` WHERE id = 9 AND datediff(dd,cash_date,'2021-06-30 08:48:11') = 0 AND cash_state = 0 ORDER BY id DESC LIMIT 0,10
Params:
array ( )
SQL Error:
Incorrect parameter count in the call to native function 'datediff'
--------问题补充----------
问题1:语句中的dd热心网友解答需要去掉,去掉dd后如何实现按天、月、年这种时间单位来查询; 如下面的使用场景: a、datediff(day,cash_date,$today)<=30 "; //30天内 b、datediff(month,cash_date,$today)=0 "; //本月 c、datediff(year,cash_date,getdate())=1 "; //去年
问题2:datediff(dd,cash_date,getdate()) ,该语句中当前时间获取方式getdate()写法是否错误,应该用什么写法?
问题3:注意:本问题要查询的数据表中cash_date
字段值的类型是“2021-06-11 20:10:09”,所以第二个参数是否也要用'Y-m-d H:i:s'的时间格式对应上?