简体   繁体   中英

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. 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)

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.

Where is my error ?

MySQL is seeing the subquery return only a single field/row, and therefore treats it as something like:

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

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

For an 'in' query, there's no need for the group_concat stuff, simply do:

... 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' ...

Use 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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