gpt4 book ai didi

mysql - PHP SQLSTATE[42000] 语法错误或访问冲突 : 1064 in query

转载 作者:行者123 更新时间:2023-11-29 18:50:50 26 4
gpt4 key购买 nike

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

SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解正确的语法,以在第 1 行的“users”中使用“count(distinct exams.user_id) as count_user_mhs”,其中“users.type”(SQL:选择“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` 内连接`universities`.`id` = `exams`.`university_id` 上的 `universities` 内部联接 `users` 上的 `users`。`id` = `exams`.`user_id`(如果存在)(从 `universities` 中选择 *,其中`exams`.`university_id` = `universities`.`id`) 按 `user_id` 分组,按 `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 行。

要解决此问题,您需要像这样更新 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 子查询并不是一个好主意,因为它们将为主查询选择的每一行执行.

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com