2014-01-10 103 views
2

这个MySQL查询有什么问题?子查询中的MySQL语法错误

$result = mysql_query("SELECT did FROM publications where group IN 
(SELECT s_group FROM subscriptions where uid1='$id')") or die(mysql_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 
+0

由于[语法错误的可能重复在MySQL中使用保留字作为表或列的名称](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or -column-name-in-mysql) –

回答

3

GROUP是保留关键字。周围`将意味着这是一个列名:

SELECT did FROM publications where `group` IN 
(SELECT s_group FROM subscriptions where uid1=1) 
+1

我认为最好的做法是始终使用它们。 – AmazingDreams

+1

但是,解析器不会在'group'中出现......它在's_group'上有问题吗?建议OP要么没有发布他的确切查询,要么发布他确切的错误消息。谨防。 – eggyal

+0

@eggyal好点!我只会剔除错误。 – Jim

1

你必须写像group“组”列中,因为关键字组在MySQL的保留关键字。

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

或者你也可以用户别名

$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

关注你@paulius说,然后检查UID1的数据类型,如果它是不是从$ id取消报价数量

+0

这不提供问题的答案。要批评或要求作者澄清,在他们的帖子下留下评论 - 你总是可以评论你自己的帖子,一旦你有足够的[声誉](http://stackoverflow.com/help/whats-reputation),你会能够[评论任何帖子](http://stackoverflow.com/help/privileges/comment)。 – Cris

+0

我无法评论作者的帖子,这就是为什么我在这里回答,现在我可以在这里评论。 –