sql查询在Workbench中工作,但在Larevel中不起作用

时间:2017-03-17 08:28:11

标签: mysql laravel group-by sql-order-by laravel-5.3

我有一个问题:

SELECT DATE_FORMAT(created_at,'%Y-%m') as x, COUNT(created_at) as y FROM table GROUP BY DATE_FORMAT(created_at,'%Y-%m') ORDER BY id DESC LIMIT 12;

它适用于MySQL Workbech,但是当我使用DB::select();在Laravel中粘贴查询时返回错误:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'user.table.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: SELECT DATE_FORMAT(created_at,'%Y-%m') as x, COUNT(created_at) as y FROM table GROUP BY DATE_FORMAT(created_at,'%Y-%m') ORDER BY id DESC LIMIT 12;)

当我删除ORDER BY id DESC时,它完美无缺,但我需要反向表并仅选择最后12行。任何想法如何解决它?

1 个答案:

答案 0 :(得分:0)

您正在使用group by,而id中的group by则不同,因此不会在此查询中选择。 您可以通过x

使用订单