2

What's wrong with this MySQL query?

    $result = mysql_query("SELECT did FROM publications where group IN 
(SELECT s_group FROM subscriptions where uid1='$id')") or die(mysql_error());

I am getting syntax 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 's_group FROM 
subscriptions where uid1='34846')' at line 1
user
  • 751
  • 2
  • 10
  • 18
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Ian Ringrose May 06 '14 at 10:13

3 Answers3

3

GROUP is a reserved keyword. Surrounding with ` will signify it's a column name:

SELECT did FROM publications where `group` IN 
(SELECT s_group FROM subscriptions where uid1=1)
Jim
  • 22,354
  • 6
  • 52
  • 80
  • 1
    Best practice I think is to always use them. – AmazingDreams Jan 10 '14 at 10:45
  • 1
    But the parser did not balk at `group`... it had an issue at `s_group` ? Suggests the OP has either not posted his exact query, or his exact error message. Beware. – eggyal Jan 10 '14 at 10:46
1

you have to write "group" column like group, because keyword group is reserved keyword in MySQL.

$result = mysql_query("SELECT did FROM publications where `group` IN 
(SELECT s_group FROM subscriptions where uid1='$id')") or die(mysql_error());

Or you can user alias

$result = mysql_query("SELECT did FROM publications p where p.group IN 
(SELECT s_group FROM subscriptions where uid1='$id')") or die(mysql_error());
0

follow what @paulius said and then check data type of uid1 if it's number than remove quotes from $id

Pradeep Sharma
  • 327
  • 4
  • 15
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Cris Jan 10 '14 at 11:15
  • I can't comment on an author's post that is why I answered here and now I can comment here. – Pradeep Sharma Jan 10 '14 at 11:30