3
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 ?

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
MatterGoal
  • 16,038
  • 19
  • 109
  • 186

3 Answers3

9

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)
Marc B
  • 356,200
  • 43
  • 426
  • 500
0
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' ...

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
0

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))
Pang
  • 9,564
  • 146
  • 81
  • 122
sam
  • 31
  • 1
  • 1
  • 8