我正在尝试使用laravel 5.3构建查询。但是当我进行此查询时,我收到此错误。
错误:
SQLSTATE [42000]:语法错误或访问冲突:1055'laravel.location.locationDate'不在GROUP BY中(SQL:select count(*),location {
location
}tagCode
= 24930和xLocation
> -1和xLocation
< 194和yLocation
> 60和yLocation
< 190和created_at
> 2017-03 -09 00:00:01和created_at
< 2017-03-09 23:59:59分组按日期(locationDate),小时(locationDate))
顺便说一句,如果我复制查询并尝试在sql中运行它的工作。但我只是在“2017-03-09 00:00:01”
中为created_at添加引号这是我的代码..
$zoneTime = DB::table('location')
->select(DB::raw('count(*), locationDate'))
->where('tagCode', $tagCode)
->where('xLocation', '>', $workingZone->x1)
->where('xLocation', '<', $workingZone->x3)
->where('yLocation', '>', $workingZone->y3)
->where('yLocation', '<', $workingZone->y1)
->where('created_at', '>', $startDate)
->where('created_at', '<', $endDate)
->groupBy(DB::raw('DATE(locationDate)'))
->groupBy(DB::raw('hour(locationDate)'))
->get();
答案 0 :(得分:1)
您必须使用clausel在组中使用相同的字段,如下所示:
$zoneTime = DB::table('location')
->select(DB::raw('count(*), locationDate'))
->where('tagCode', $tagCode)
->where('xLocation', '>', $workingZone->x1)
->where('xLocation', '<', $workingZone->x3)
->where('yLocation', '>', $workingZone->y3)
->where('yLocation', '<', $workingZone->y1)
->where('created_at', '>', $startDate)
->where('created_at', '<', $endDate)
->groupBy(DB::raw('DATE(locationDate) as locationDate'))
->groupBy(DB::raw('hour(locationDate)'))
->get();
答案 1 :(得分:1)
我在config / database中更改了strict = false并且它工作正常。