MySQL,使用 find_in_set 返回的行未按预期工作

MySQL, returning row using find_in_set is not working as expected

我正在尝试 运行 以下 MySQL 命令:

SELECT ID, intervention_post_title, linked_article FROM wp_cpt_combined WHERE FIND_IN_SET (72, habitat_type)

在我的数据库中,列 habitat_type(因为这是一个多选字段)将值存储为“

id  habitat_type
1   [72, 74]
2   [70]
3   [71]

上面的 sql 命令 returns 没有行 - 我错过了什么?我需要能够使用 find_in_set (72, habitat_type) 或 find_in_set (74, habitat_type) 等来拉回第 1 行。我是否必须替换为删除方括号?

如有任何帮助,我们将不胜感激。

D.

  • 您可以使用 REPLACE() 函数将出现的 [] 替换为空字符串。

尝试以下操作:

SELECT 
  ID, 
  intervention_post_title, 
  linked_article 
FROM wp_cpt_combined 
WHERE FIND_IN_SET ('72', REPLACE(REPLACE(habitat_type, '[', ''), ']', ''))

去掉方括号即可 在集合中查找像 Find_in_set(72,'72,73,74') 它将 return 72 的位置,即 1 而 find_in_set(72,'73,72,74') 即 2 所以你也可以尝试使用条件运算符 find_in_set(72,'72,73')>0