views:

39

answers:

4

I have two MySQL tables: Group(gr_id, gr_name, gr_description, parent_id) Group_has_User(User_id, Group_id)

I'm trying to execute the query:

SELECT group.gr_id, group.gr_name, group.gr_description, group.parent_id 
FROM group, Group_has_User AS gu
WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1

It gives an error: 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, Group_has_User AS gu WHERE (group.gr_id = gu.Group_id) AND gu.User_id = 1' at line 1

How should I write it correct?

+1  A: 

group is a keyword in SQL. Try giving your tables more sensible names, or using:

SELECT g.gr_id, g.gr_name, g.gr_description, g.parent_id 
    FROM `group` g, Group_has_User AS gu
    WHERE (g.gr_id = gu.Group_id) AND gu.User_id = 1
Borealid
+4  A: 

group is a keyword in SQL. Enclose such names in backticks

FROM `group`, Group_has_User AS gu
Col. Shrapnel
A: 

Maybe you must write 'Group', not 'group'.

Mishuko
In that case there will be different error message. Don't you read?
Col. Shrapnel
A: 

Try this. Remove the "AS" keyword after the table name Group_has_User and execute the query

Anand Devaraj