简体   繁体   English

如何在mysql中将逗号分隔的参数转换为行?

[英]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 在我的函数中,我使用了IN关键字, where有一个参数值

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 , 现在我先通过“ 1,8,9”,然后结果集仅以1为基础,

same as second case '9,8,7,4,5' then result set comes basis on only 9 , 与第二种情况'9,8,7,4,5'相同,则结果集仅基于9出现,

I think 'where' condition with 'IN' keyword consider only first ID 我认为带有“ IN”关键字的“ where”条件仅考虑第一个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() : 将事物存储在列表in时, in的替换是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. 您应该将列表作为in或子查询放入SQL in ,以使用索引。

You need to give the IN() clause seperate values and not a single one that has a list in it. 您需要为IN()子句提供单独的值,而不是单个包含列表的值。 This would work: 这将工作:

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

In MySQL you can use the FIELD() function instead: 在MySQL中,您可以改用FIELD()函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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