2014-01-10 98 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

我無法評論作者的帖子,這就是爲什麼我在這裏回答,現在我可以在這裏評論。 –