子查询中的MySQL语法错误

时间:2014-01-10 10:39:04

标签: 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

3 个答案:

答案 0 :(得分:3)

GROUP是保留关键字。围绕`将表示它是一个列名:

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

答案 1 :(得分:1)

你必须编写像group这样的“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());

答案 2 :(得分:0)

按照@paulius的说法检查,然后检查uid1的数据类型(如果是数字),而不是从$ id中删除引号