Yogesh Prajapati
Yogesh Prajapati

Reputation: 4870

Use GROUP_CONCAT result in IN Clause Mysql

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.

Upvotes: 12

Views: 8460

Answers (2)

Filipe Silva
Filipe Silva

Reputation: 21657

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

Upvotes: 10

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

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

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

Upvotes: 2

Related Questions