mySQL SELECT IN from string

mysql

Solution

You may use FIND_IN_SET instead of just `IN`, normal `IN` keyword couldn't search between comma seperated values within one field.

For example

mysql> select FIND_IN_SET(4, replace('4|6|8|','|',','));

+-------------------------------------------+
| FIND_IN_SET(4, replace('4|6|8|','|',',')) |
+-------------------------------------------+
|                                         1 |
+-------------------------------------------+
1 row in set (0.00 sec)

Problem

Here is my table X: ``` id vals --------------------- 1 4|6|8| ``` Now table Y: ``` id name -------------------- 1 a 4 b 6 c 8 d ``` Now I want the following: ``` select * from Y where id IN (replace(select vals from X where id = '1'),'|',',') ``` But this does not seem to work. Any ideas why?

Original source