我有一个查询,我想通过shipping_date订购,但我收到此错误:
QueryException in Connection.php line 770:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression 1 of
ORDER BY clause is not in GROUP BY clause and contains nonaggregated
column 'device.devices.shipping_date' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by (SQL: select count(*) as device,
DATE_FORMAT(shipping_date,"%M %Y") as year from `devices` where
`shipping_date` is not null and `internal_use` = 0 group by `year` order by `shipping_date` asc)
我的代码:
$sold_devices = Device::selectRaw('count(*) as device, DATE_FORMAT(shipping_date,"%M %Y") as year')
->whereNotNull('shipping_date')
->where('internal_use', '=', 0)
->groupBy('year')
->orderBy('shipping_date', 'asc')
->pluck('device', 'year');
任何帮助?
由于
答案 0 :(得分:1)
您必须在select
中指定确切的group by
列:在您的情况下
->groupBy('DATE_FORMAT(shipping_date,"%M %Y")')
->orderBy('DATE_FORMAT(shipping_date,"%M %Y")', 'asc')
一般来说,您不能在where
,group by
和order by
子句中使用列别名。
修改强>
要更改订购标准,您可以使用月份的数值而不是其名称:
->groupBy('DATE_FORMAT(shipping_date,"%M %Y")')
->orderBy('DATE_FORMAT(shipping_date,"%Y%c")', 'asc')
答案 1 :(得分:0)
让我们试一试。
它会起作用......
->groupBy(DB::raw('DATE_FORMAT(shipping_date,"%M %Y")'))
->orderBy(DB::raw('DATE_FORMAT(shipping_date,"%M %Y"))','asc')