12

I have a Query which returns comma separated integers like :

select GROUP_CONCAT(ids) from table2

now I want to use that result in another query like :

select * from table1 where column in (select GROUP_CONCAT(ids) from table2)

in this case it will consider only first value in IN clause.

2 Answers 2

10

I agree with @Alma that this can't be done with IN, you might be able to do it with FIND_IN_SET, but if you can do it with IN it's probably a better approach :

SELECT *
FROM table1
WHERE find_in_set(ids, (
      SELECT GROUP_CONCAT(ids)
      FROM table2
      )) != 0;

sqlfiddle demo

Sign up to request clarification or add additional context in comments.

3 Comments

its not a good solution when you can directly use IN() , GROUP_CONCAT is not advisable because it has a character limit you need to increase that one if you used in your answer you need to brief also,when it can be done in simple way then why make set first then apply find_in_set it makes sense ?
i'm not saying it is a good approach. i'm advising to use IN instead, but i think this is what he was asking for.
is there any other way, to use in in where clause for the group_concat() values other then find_in_set
2

Any special reason not using a join and using a sub query

select * from table1 t1
JOIN table2 t2 on (t2.column = t1.ids)

2 Comments

why should this be done?
These types of queries(group_concat+find_in_set) are useful or needed when you are storing the output of group_concat in a variable and re-using them multiple times on different places in programmatic approach, thereby skipping additional JOIN load on multiple query to improve performance, generally in STORED PROCEDURES and/or FUNCTIONS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.