2

Rediculous but I cant find error in this request

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

But SQL says

#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

WTH?

juergen d
  • 195,137
  • 36
  • 275
  • 343
Anton A.
  • 1,646
  • 13
  • 34

2 Answers2

3

group is a reserved keyword in MySQL and needs to be escaped by backticks.

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
juergen d
  • 195,137
  • 36
  • 275
  • 343
0

Its not good to use reserved keyword as a table or a column name.. group is a reserved keyword and that's why giving an error, you can use quotes tilt (`) to use it as a table name.

Also you can't use it as column name, see the related post:

group as a column name in insert command

Community
  • 1
  • 1
Aman Aggarwal
  • 16,184
  • 9
  • 53
  • 77