找不到 mySQL 请求错误

Cant find mySQL request 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 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'group ON student.group_id = group.group_id LIMIT 0, 30' at line 3

与?

group 是一个 reserved 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

将保留关键字用作 table 或列名是不好的.. group 是保留关键字,这就是为什么会出现错误,您可以使用引号倾斜 (`) 将其用作 table 名称。

也不能作为列名使用,见相关post:

group as a column name in insert command