0

What is wrong here?

SELECT u.id
FROM user u
LEFT JOIN group g ON g.id=u.group_id

The following error is returned

#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 'group g ON g.id=u.group_id LIMIT 0, 30' at line 3
clarkk
  • 27,151
  • 72
  • 200
  • 340

3 Answers3

3

GROUP is a reserved word in the SQL standard due to the GROUP BY clause. Even Stack Overflow highlights it with blue ;)

If you want to use group as an identifier, you must quote it like this:

`group`

You may also rename the table to something more meaningful, like UserGroup. That will solve your issue as well.

Dmytro Shevchenko
  • 33,431
  • 6
  • 51
  • 67
1

"group" is a reserved keyword of MySQL.

You can avoid this error with the next query:

SELECT u.id
FROM user u
LEFT JOIN `group` g ON g.id=u.group_id

It's recommended to avoid using "group" as a table name.

mvillaress
  • 489
  • 3
  • 6
1

'group' is a keyword of SQL. You can try this:

SELECT u.id
FROM 'user' u
LEFT JOIN 'group' g ON u.group_id = g.id
Blue Smith
  • 8,580
  • 2
  • 28
  • 33