简体   繁体   English

MySQL:如何将字符串“ 1,2,3,4”转换为(1,2,3,4),以便能够在“ where X in()”语句中使用它

[英]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的答案。

Well first of all, this usually means that your database structure is not good; 首先,这通常意味着您的数据库结构不好; you should normalize your database. 您应该规范化数据库。

However, you can do what you want, with the FIND_IN_SET function: 但是,您可以使用FIND_IN_SET函数执行所需的操作:

SELECT * FROM tbl WHERE NOT FIND_IN_SET(name, '1,2,3,4')

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.

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