在Mysql中由DATE_FORMAT错误的组合

时间:2018-06-02 14:32:46

标签: mysql sql laravel

有以下查询模型:

return \DB::table('clients')->select(DB::raw("
            DATE_FORMAT(result_test.created_at, '%M %e %Y') AS day,
            COUNT(result_test.id) AS passed"))
            ->leftJoin('result_test', 'result_test.client_id', '=', 'clients.id')
            ->where('clients.doctor_id', $user_id)
            ->whereBetween('result_test.created_at', array($startDate, $endDate))
            ->orderBy('result_test.created_at')
            ->groupBy('day')
            ->get();

请注意:

DB::raw("
                DATE_FORMAT(result_test.created_at, '%M %e %Y') AS day,
                COUNT(result_test.id) AS passed")

之后,我尝试按AS day分组行:

->groupBy('day')

我收到此错误消息:

42000/1055 - db.result_test.created_at' isn't in GROUP BY

查询有什么问题?

当我删除它时:->orderBy('result_test.created_at')它就可以了。为什么呢?

1 个答案:

答案 0 :(得分:1)

您收到此消息是因为'result_test.created_at'不在选择组中。

return \DB::table('clients')->select('result_test.created_at',DB::raw("
            DATE_FORMAT(result_test.created_at, '%M %e %Y') AS day,
            COUNT(result_test.id) AS passed"))
            ->leftJoin('result_test', 'result_test.client_id', '=', 'clients.id')
            ->where('clients.doctor_id', $user_id)
            ->whereBetween('result_test.created_at', array($startDate, $endDate))
            ->orderBy('result_test.created_at')
            ->groupBy('day')
            ->get();

希望这有帮助。