我正在运行查询,但是当我运行它时,它返回一个

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' count(distinct exams.user_id) as count_user_mhs from `users` where `users.type`' at line 1 (SQL: select `universities`.`name`, count(distinct exams.user_id) as count_user_dsn from `users` where `users.type` = 0, count(distinct exams.user_id) as count_user_mhs from `users` where `users.type` = 1, count(exams.subject_id) as count_subject, sum(exams.score) as count_score, TIME_FORMAT(SUM(TIMEDIFF(exams.exam_end_date, exams.exam_start_date)), "%H:%i:%s") as count_time from `exams` inner join `universities` on `universities`.`id` = `exams`.`university_id` inner join `users` on `users`.`id` = `exams`.`user_id` where exists (select * from `universities` where `exams`.`university_id` = `universities`.`id`) group by `user_id` order by `count_score` desc, `count_time` asc)

$exams = Exam::join('universities', 'universities.id', '=', 'exams.university_id')
                ->join('users', 'users.id', '=', 'exams.user_id')
                ->select('universities.name',DB::raw('count(distinct exams.user_id) as count_user_dsn from `users` where `users.type` = 0'),DB::raw('count(distinct exams.user_id) as count_user_mhs from `users` where `users.type` = 1'),DB::raw('count(exams.subject_id) as count_subject'),DB::raw('sum(exams.score) as count_score'),DB::raw('TIME_FORMAT(SUM(TIMEDIFF(exams.exam_end_date, exams.exam_start_date)), "%H:%i:%s") as count_time'));


有人可以帮忙吗?我猜想,这与DB::raw表达式有关。但是我不知道到底是什么问题。

最佳答案

您生成的查询如下所示:

select
    `universities`.`name`,
    count(distinct exams.user_id) as count_user_dsn from `users` where `users.type` = 0,
    count(distinct exams.user_id) as count_user_mhs from `users` where `users.type` = 1,
    count(exams.subject_id) as count_subject,
    sum(exams.score) as count_score,
    TIME_FORMAT(SUM(TIMEDIFF(exams.exam_end_date, exams.exam_start_date)), "%H:%i:%s") as count_time
from
    `exams`
        inner join
    `universities` on `universities`.`id` = `exams`.`university_id`
        inner join
    `users` on `users`.`id` = `exams`.`user_id`
where
    exists (select * from `universities` where `exams`.`university_id` = `universities`.`id`)
group by
    `user_id`
order by
    `count_score` desc,
    `count_time` asc


问题出在DB::raw代码生成的第3行和第4行上。

要解决此问题,您需要像这样更新您的口才查询:

<?php
$exams = Exam::join('universities', 'universities.id', '=', 'exams.university_id')
    ->join('users', 'users.id', '=', 'exams.user_id')
    ->select(
        'universities.name',
        DB::raw('(select count(distinct exams.user_id) as count_user_dsn from `users` where `users.type` = 0)'),
        DB::raw('(select count(distinct exams.user_id) as count_user_mhs from `users` where `users.type` = 1)'),
        DB::raw('count(exams.subject_id) as count_subject'),DB::raw('sum(exams.score) as count_score'),
        DB::raw('TIME_FORMAT(SUM(TIMEDIFF(exams.exam_end_date, exams.exam_start_date)), "%H:%i:%s") as count_time')
    );


就是说...在主SELECT COUNT语句中执行SELECT子查询不是一个好主意,因为它们将针对主查询选择的每一行执行。

关于mysql - PHP SQLSTATE [42000]语法错误或访问冲突:查询中为1064,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44364383/

10-09 06:47