Friday, March 17, 2017

sql query works in Workbench, but not in Larevel

I've got a query:

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;

It works in MySQL Workbech, but when i paste query in Laravel using DB::select(); return error below:

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;)

When I delete ORDER BY id DESC it works perfectly, but I need to reverse table and select only 12 last rows. Any ideas how to fix it?



via jdoe

Advertisement