1

How can I use an string input as 1,2,3,4 taken from a SELECT to put in on an IN condition?

I mean, having this output from this:

SELECT id_list
FROM ids
where categories = 4;

id_list is a string like 1,2,3,4.

Now I want to use that string to put in inside a IN ():

SELECT category_name
FROM categories
WHERE categories IN (
    SELECT id_list
    FROM ids
    where categories = 4
)

But this only outputs the result of the first value of the string, in this case, 1.

I know it's not the best implementation, but I'm not the one who made the DB design and I need to make the query this way. This is just a simplified example.

Can someone tell me how con I archieve my goal? Thanks in advance!

Avión
  • 7,963
  • 11
  • 64
  • 105

1 Answers1

2

You can use FIND_IN_SET

Query

SELECT `category_name`
FROM `categories`
WHERE FIND_IN_SET(`id`, (SELECT `id_list` FROM `ids` WHERE `categories` = 4)) > 0;

Find a fiddle demo here

Ullas
  • 11,450
  • 4
  • 33
  • 50