[英]MySQL: how can i convert a string '1,2,3,4' to a (1,2,3,4) so i'll be able to use it in a 'where X in ()' statement
I ran a query that resulted in the string '1,2,3,4'
. 我运行了一个查询,结果为字符串
'1,2,3,4'
。
How can I run a second query that treats that string as a list of numbers. 我如何运行第二个查询,将该字符串视为数字列表。 So I'll be able to do:
这样我就可以做到:
select * from tbl where name not in (1,2,3,4)
I would like an answer in pure MySQL. 我想要一个纯MySQL的答案。
Use FIND_IN_SET : 使用FIND_IN_SET :
select * from tbl where FIND_IN_SET(name, '1,2,3,4') = 0
Like the other answer, I would also recommend normalizing your database if at all possible. 像其他答案一样,如果可能的话,我也建议您规范化数据库。 This query could be slow as it will require a scan of the table.
该查询可能很慢,因为它将需要扫描表。 Even if there is an index on name this query won't be able to use it efficiently.
即使名称上有索引,该查询也将无法有效地使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.