使用mysql函数查询时报错
原生sql:
SELECT id,WEEKDAY(DAY) AS wd,DATE_FORMAT(DAY,'%m-%d') AS dd
FROM sss
WHERE YEARWEEK(DATE_FORMAT(DAY,'%Y-%m-%d') - INTERVAL 1 DAY) = YEARWEEK(NOW() - INTERVAL 1 DAY)
我用laravel写的时候报错了。。。
$res = DB::table('ss')
->select('id','WEEKDAY(DAY) AS wd',"DATE_FORMAT(DAY,'%m-%d') AS dd")
->where("YEARWEEK(DATE_FORMAT(DAY,'%Y-%m-%d') - INTERVAL 1 DAY)" , "YEARWEEK(NOW() - INTERVAL 1 DAY)")
->get();
报错信息
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'WEEKDAY(DAY)' in 'field list' (SQL: select `id`, `sid`, `WEEKDAY(DAY)` as `wd`, `DATE_FORMAT(DAY,'%m-%d')` as `dd`, `stepnum` from `sport_stepnum` where `YEARWEEK(DATE_FORMAT(DAY,'%Y-%m-%d') - INTERVAL 1 DAY)` = YEARWEEK(NOW() - INTERVAL 1 DAY) and `sid` is null)
想问
WEEKDAY(DAY) 被加上 反引号 “ ` ”,且date_format里面的参数乱码
laravel新手,求高手解答 谢谢!!!!!
1 Comment
你这种复杂的语句用:
这种原生查询更合适:https://xueyuanjun.com/post/9576.html#toc_4,使用查询构建器显得麻烦