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。但是,如果我启动整个查询,我只获得一行。
但是,如果我尝试用其值(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行......正如我所需要的那样。
我的错误在哪里?
答案 0 :(得分:8)
MySQL看到子查询只返回一个字段/行,因此将其视为:
... and A.id IN ('1,2,3,4')
归结为A.id = '1,2,3,4'
。
对于'in'查询,不需要group_concat,只需执行:
... and A.id IN (select B.id FROM b where user = 3)
答案 1 :(得分:0)
SELECT name
FROM test
WHERE salry
IN (
SELECT GROUP_CONCAT( CONCAT('''', salry,'''' ) )
FROM test group by salry
)
这个concat将使用单引号追加结果集,但仍然不能像salry一样工作在结果集'1000','2000','3000','40000'
...
答案 2 :(得分:0)
使用FIND_IN_SET()
SELECT A.id, A.title,
FROM (`table`) as A
WHERE A.active = '1'
AND FIND_IN_SET(A.id,(SELECT GROUP_CONCAT(B.id) from B where user = 3))