mysql - 使用逗号分隔列表在列中查找逗号分隔值

标签 mysql sql mysql-5.7 find-in-set

<分区>

我有一个 item_id_list 列,其中包含逗号分隔的 id,如下所示:

enter image description here

我可以使用 FIND_IN_SET() 搜索它们,如下所示:

SELECT * FROM task_detail WHERE FIND_IN_SET('21', item_id_list)

但我需要检查该行中是否确实存在多个 id,但添加另一个 id 进行搜索将不会像文档中所述那样工作。

https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_find-in-set

This function does not work properly if the first argument contains a comma (,) character.

SELECT * FROM task_detail WHERE FIND_IN_SET('21, 79', item_id_list)

在不改变我的表结构的情况下,如何解决这个问题?现在通过 id 数循环 FIND_IN_SET() 是解决方案。

最佳答案

您可以对 find_in_set 使用 OR

    SELECT * FROM task_detail 
    WHERE FIND_IN_SET('21', item_id_list)
    OR  FIND_IN_SET('79', item_id_list)

关于mysql - 使用逗号分隔列表在列中查找逗号分隔值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54669414/

相关文章:

MySQL:ORDER BY 使用长字符串产生错误的顺序

mysql - 从表中选择相等或最接近的较大数字?

php - 返回存储为 utf8_general_ci 和/或 utf8_unicode_ci 的数据库结果时,是否应该使用 utf8_decode?

php - beginTransaction() 可以与prepare() 一起使用吗?

c++ - 如何使用 ODBC 处理长字符串?

sql - 如何在sql server中设置两个具有相同值的局部变量?

sql - 如何将数据从 MongoDB 迁移到 SQL-Server?

ubuntu - 错误 2003 (HY000) : Can't connect to MySQL server on '10.0.0.5' (111)

mysql - 如何在 MySql 中检查数据字典的大小

php - 在mysql中总结多行