在mysql select查询中匹配分隔值字段


Match delimeter(|) separated values field in mysql select query

在我的mysql表中,其中一个字段的值如(1|2|3)存储在其中。

  id   Skills
 ----  --------
   1   1|2|3|5
   2     2|4|5
   3    4|6|3
   4    5|2|3|1

我想搜索并列出基于匹配技能的id。如果我想列出包含3的技能的id,我必须列出id 1,3,4。就像如果我想列出包含1,5 (either 1 nor 5 or both)的技能的id(如多个值1,4,3),那么我想列出1,2,4(i want to list 2 also)。如有任何帮助,不胜感激。

使用FIND_IN_SET

select id
from your_table
where find_in_set('3', replace(skills, '|',',') > 0
select id
from your_table
where find_in_set('3', replace(skills, '|',',')) > 0
or find_in_set('5', replace(skills, '|',',')) > 0

但是你应该改变你的DB结构。始终将单个值存储在列中以避免此类问题!

这可能有帮助:

SELECT * FROM YOUR_TABLE
WHERE Skills REGEXP '[[:<:]]1[[:>:]]' = 1
OR Skills REGEXP '[[:<:]]5[[:>:]]' = 1