2011-06-19 216 views
2
SELECT A.id, A.title, 
FROM (`table`) as A 
WHERE A.active = '1' 
AND A.id IN (SELECT GROUP_CONCAT(B.id) from B where user = 3) 

如果我僅啓動子查詢SELECT GROUP_CONCAT(B.id) from B where user = 3,則獲得1,2,3,4。但是,如果我啓動整個查詢,我只獲得一行。IN子查詢中的GROUP_CONCAT

但如果我嘗試用它的值(1,2,3,4)

SELECT A.id, A.title, 
FROM (`table`) as A 
WHERE A.active = '1' 
AND A.id IN (1,2,3,4) 

替代子查詢我獲得4行...因爲我需要。

我的錯誤在哪裏?

回答

7

MySQL是看到子查詢只返回一個字段/行,因此將其視爲是這樣的:

... and A.id IN ('1,2,3,4') 

其歸結爲A.id = '1,2,3,4'

對於「在」查詢,沒有必要爲GROUP_CONCAT的東西,簡單地做:

... and A.id IN (select B.id FROM b where user = 3) 
0
SELECT name 
FROM test 
WHERE salry 
IN (

SELECT GROUP_CONCAT(CONCAT('''', salry,'''')) 
FROM test group by salry 
) 

這CONCAT將追加單引號還是它不工作像salry將在結果結果集'1000','2000','3000','40000' ...

0

使用FIND_IN_SET()

SELECT A.id,A.title, FROM(table)爲A WHERE A.active ='1' AND FIND_IN_SET(A.id,(SELECT GROUP_CONCAT(B.id)from B where user = 3))