7

Table_a

column name: list_id
record 1: 1,2,5,6,8
record 2: 1,3,2
record 3: 6,7,2
record 4: 9,8,0

Table_b

id ='2';

How to select records that have id='2' in the comma separated string? From the above example it should return record 1,2 and 3.

Query(How to amend this query, please?):

SELECT * FROM Table_a,Table_b WHERE Table_b.id = Table_a.list_id;
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
112233
  • 2,406
  • 3
  • 38
  • 88

2 Answers2

3

use find_in_set function, but it is not optimized way you should normalize your data.

SELECT * FROM 
Table_a AS a 
JOIN Table_b AS b ON FIND_IN_SET(b.id,a.list_id)
Zafar Malik
  • 6,734
  • 2
  • 19
  • 30
0

This should work for you

SELECT * FROM Table_a,Table_b WHERE Table_b.id = Table_a.list_id and 
concat(',',Table_a.list_id,',') like '%,2,%'
ManiMuthuPandi
  • 1,594
  • 2
  • 26
  • 46