我知道这是一个已知问题,因此还有其他关于此问题的问题,但是没有一个有用。如果我直接对数据库运行时原始查询工作正常,则抛出Laravel会给出错误。 到底是怎么了?
查询:
Order::selectRaw("COUNT(id) AS `data`, DATE_FORMAT(created_at, '%m-%Y') AS new_date, YEAR(created_at) AS `year`, MONTH(created_at) AS `month`")
->groupBy('year')->get();
错误:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'orders.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select COUNT(id) AS `data`, DATE_FORMAT(created_at, '%m-%Y') AS new_date, YEAR(created_at) AS `year`, MONTH(created_at) AS `month` from `orders` group by `year`)
答案 0 :(得分:0)
这是数据库错误,与Laravel不相关。在数据库中运行完全相同的查询也应该失败,如果没有失败,则应该:)。
您还需要按查询中的其他字段“ new_date”,“ month”和“ year”进行分组。尝试这样的事情:
Order::selectRaw("COUNT(id) AS `data`, DATE_FORMAT(created_at, '%m-%Y') AS new_date, YEAR(created_at) AS `year`, MONTH(created_at) AS `month`")
->groupBy('year', 'month', 'new_date')->get();
答案 1 :(得分:0)
如果在数据库上运行时没有任何问题,则这是文件 config-> database.php 中的laravel设置,只需将'strict'
设置为{{ 1}}。
'false'