【问题标题】:Laravel SQLSTATE[42000]: Syntax error or access violation: 1055Laravel SQLSTATE[42000]:语法错误或访问冲突:1055
【发布时间】:2017-07-31 01:48:27
【问题描述】:

我正在尝试使用 laravel 5.3 构建查询。但是当我进行此查询时出现此错误。

错误:

SQLSTATE[42000]: 语法错误或访问冲突: 1055 'laravel.location.locationDate' 不在 GROUP BY 中(SQL: select count(*), locationDate from location where tagCode = 24930 and @ 987654323@ > -1 and xLocation yLocation > 60 and yLocation created_at > 2017-03-09 00:00:01 and created_at

顺便说一句,如果我复制查询并尝试在 sql 中运行它,它的工作原理。但我只是在 created_at 中添加引号,例如 '2017-03-09 00:00:01'

这是我的代码..

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

【问题讨论】:

    标签: php mysql sql-server laravel laravel-5.3


    【解决方案1】:

    我在 config/database 中更改了 strict = false 并且它起作用了。

    【讨论】:

    • 这是一个临时解决方案,您可能应该更新您的查询。
    【解决方案2】:

    您必须像这样在 group by 子句中使用相同的字段:

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

    【讨论】:

      猜你喜欢
      • 2019-06-06
      • 2018-05-31
      • 2020-06-16
      • 2017-02-01
      • 2017-04-01
      • 2023-04-01
      • 1970-01-01
      • 2017-10-29
      • 2015-09-20
      相关资源
      最近更新 更多