简体   繁体   中英

How to convert comma separated parameters to rows in mysql?

In my function I used IN key word with where I have one parameter value

eg:

CASE- 1
 '1,8,9' or

CASE- 2
 '9,8,7,4,5'   or

CASE- 3
 '9,8' 

now I get pass first '1,8,9' then result set Comes basis on only 1 ,

same as second case '9,8,7,4,5' then result set comes basis on only 9 ,

I think 'where' condition with 'IN' keyword consider only first ID

Now my question is what should I do for comma separated to rows values?

The replacement for in when things are stored in a list is find_in_set() :

where find_in_set(col, '1,2,3') > 0

Note that this cannot take advantage of an index, so it is not recommended on larger tables. You should put the list into the SQL, either as an in or as a subquery, to make use of indexes.

You need to give the IN() clause seperate values and not a single one that has a list in it. This would work:

where some_column in ('9','8','7','4','5' )

In MySQL you can use the FIELD() function instead:

where field(some_column, '9,8,7,4,5') > 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM