简体   繁体   English

IN子查询中的GROUP_CONCAT

[英]GROUP_CONCAT in IN Subquery

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)

If i launch subquery SELECT GROUP_CONCAT(B.id) from B where user = 3 only, i obtain 1,2,3,4. 如果我SELECT GROUP_CONCAT(B.id) from B where user = 3启动子查询SELECT GROUP_CONCAT(B.id) from B where user = 3 ,我获得1,2,3,4。 But if i launch entire query i obtain only one row. 但是,如果我启动整个查询,我只获得一行。

But if i try to substitute the subquery with its value (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)

i obtain the 4 rows ... as i need. 我获得了4行......正如我所需要的那样。

Where is my error ? 我的错误在哪里?

MySQL is seeing the subquery return only a single field/row, and therefore treats it as something like: MySQL看到子查询只返回一个字段/行,因此将其视为:

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

which boils down to A.id = '1,2,3,4' . 归结为A.id = '1,2,3,4'

For an 'in' query, there's no need for the group_concat stuff, simply do: 对于'in'查询,不需要group_concat的东西,只需执行:

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

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

this concat will append the resultset with single quotes still its not working like salry will be in resultset '1000','2000','3000','40000' ... 这个concat将使用单引号追加结果集仍然不能像salry一样工作在结果集'1000','2000','3000','40000' ......

Use FIND_IN_SET() 使用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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM