我運行一個查詢,但是當我運行它,它會返回一個PHP SQLSTATE [42000]語法錯誤或訪問衝突:在查詢
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'));
誰能幫助1064?我猜測,這與DB::raw
表達式有關。但我不知道到底出了什麼問題。
1)這不是一個警告,這是一個錯誤。 2)你沒有複製整個錯誤信息,所以我們甚至不知道可能發生了什麼問題。 3)你還沒有共享從你的代碼創建的sql查詢laravel,這也有助於識別錯誤。但是那些原始sql部分中的計數子查詢看起來是錯誤的。 4)如果你描述了你想要達到的目標,這也會有所幫助。因爲我們可以告訴你犯了什麼錯誤是一回事,但另一個想法是幫助你創建一個工作解決方案。 – Shadow
[進一步閱讀](https://stackoverflow.com/questions/23515347/how-cani-i-fix-mysql-error-1064)。 – tadman
粘貼普通錯誤消息,它會幫助調試。 – BDS