-1

If i give hard coded value inside query it works, but not in case of sub query or column given.

Here is small example of issue i am facing :

Following both query is a type of sub query, like its part of another query, so don't think that where is table 'm' and something else, as it is working already.

So, my query like :

1)

SELECT GROUP_CONCAT(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u 
  FROM users a_u 
 WHERE a_u.id IN(m.associated_admin) 
 GROUP 
    BY m.id

And m.associated_admin will return a quoted string like '1,10' so this will not work because of its a string.

2)

 SELECT GROUP_CONCAT(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u 
   FROM users a_u 
  WHERE a_u.id IN(1,10) 
  GROUP 
     BY m.id

If i write hard code like 1,10 it works, because it is not a string

So first one is not works because that query is part of another query as a sub query.

And i am sure this question couldn't be duplicate as i am facing it like in this way so any help would be appreciate, thanks reader!

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Jigar7521
  • 1,549
  • 14
  • 27

2 Answers2

4

Based on your comments, you need something like:

SELECT GROUP_CONCAT(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u 
  FROM users a_u 
 WHERE FIND_IN_SET(a_u.id, TRIM(BOTH '\'' FROM m.associated_admin)) 
 GROUP 
    BY m.id

This will first trim the quotes from m.associated_admin and then use FIND_IN_SET instead of IN so that you can use a string with comma-separated values.

jeroen
  • 91,079
  • 21
  • 114
  • 132
0

You can just create a subquery in IN for example:

SELECT group_concat(CONCAT(a_u.first_name,' ', a_u.last_name)) AS associated_admin_u 
FROM users a_u WHERE a_u.id IN(
   SELECT id FROM mytable WHERE id IN(1,10)
) GROUP BY m.id
Daan
  • 12,099
  • 6
  • 34
  • 51