mysql - Select WHERE clause, split column value and check -


i have following table schema

id   name     type 1    abc      1,2,3,4 2    pqr      2,3,5  3    xyz      1,4 4    tcs      3,1 5    ppp      2,3 

here wants result display following i.e, type 1 , 4.
result :-

id   name     type 1    abc      1,2,3,4 3    xyz      1,4 4    tcs      3,1 

you can use find_in_set() function that:

returns value in range of 1 n if string str in string list strlist consisting of n substrings. string list string composed of substrings separated “,” characters.
returns 0 if str not in strlist or if strlist empty string.

select * table1      find_in_set(1, type)   or find_in_set(4, type) 

output:

╔════╦══════╦═════════╗ ║ id ║ name ║  type   ║ ╠════╬══════╬═════════╣ ║  1 ║ abc  ║ 1,2,3,4 ║ ║  3 ║ xyz  ║ 1,4     ║ ║  4 ║ tcs  ║ 3,1     ║ ╚════╩══════╩═════════╝ 

see sqlfiddle

sqlfiddle more data


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -