无法找到mySQL请求错误

时间:2015-06-15 23:03:22

标签: mysql sql syntax-error

一丝不苟但我在这个请求中找不到错误

SELECT * FROM diploms 
LEFT JOIN student ON diploms.student_id = student.student_id 
LEFT JOIN group ON student.group_id = group.group_id
LEFT JOIN speciality ON group.speciality_id = speciality.speciality_id 
ORDER BY (CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END), speciality.name ASC

但是SQL说

  

#1064 - 您的SQL语法出错;检查与您的MariaDB服务器版本对应的手册,以便在#student; group.group_id = group.group_id LIMIT 0,30'在第3行

WTH?

2 个答案:

答案 0 :(得分:3)

groupreserved keyword in MySQL,需要通过反引号进行转义。

SELECT * 
FROM diploms 
LEFT JOIN student ON diploms.student_id = student.student_id 
LEFT JOIN `group` ON student.group_id = `group`.group_id
LEFT JOIN speciality ON `group`.speciality_id = speciality.speciality_id 
ORDER BY CASE WHEN speciality.name IS NULL THEN 1 ELSE 0 END, 
         speciality.name ASC

答案 1 :(得分:0)

将保留关键字用作表格或列名称并不好。 group是一个保留关键字,这就是给出错误的原因,你可以使用引号tilt(`)将它用作表名。

此外,您不能将其用作列名,请参阅相关帖子:

group as a column name in insert command