【问题标题】:PHP SQLSTATE[42000] syntax error or access violation: 1064 in queryPHP SQLSTATE[42000] 语法错误或访问冲突:查询中的 1064
【发布时间】:2017-11-05 22:59:21
【问题描述】:

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

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 表达式有关。但我不知道到底是哪里出了问题。

【问题讨论】:

  • 1) 这不是警告,这是错误。 2)你没有复制整个错误信息,所以我们甚至不知道可能出了什么问题。 3)您没有共享从您的代码创建的 sql 查询 laravel,这也有助于识别错误。但是那些在原始 sql 部分中有计数的子查询看起来确实是错误的。 4)如果您在这里描述了您想要实现的目标,这也会有所帮助。因为我们可以告诉您在哪里犯了错误是一回事,所以帮助您创建可行的解决方案是另一回事。
  • 粘贴纯错误信息,有助于调试。
  • 我编辑了问题,希望对您有所帮助
  • 你写作质量的最低improvement 会对你的问题有很大帮助。

标签: mysql sql laravel


【解决方案1】:

您生成的查询如下所示:

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 行。

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

<?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 主语句中执行SELECT COUNT 子查询并不是一个好主意,因为它们将针对主查询选择的每一行执行。

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 1970-01-01
    • 2014-10-30
    • 2018-07-02
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-10-29
    相关资源
    最近更新 更多