搜索以逗号分隔的 mysql 列

Searching on a comma separated mysql column

我正在从事一个与其他人一起开始的项目。在数据库中,开发人员选择使用逗号分隔的 table 将 1 to many 关系保存在单个 table 上,而不是使用单独的 table。 table结构是这样的

CREATE TABLE pages(
  pageid INT(6) AUTO_INCREMENT PRIMARY KEY,
  newsid INT(6),
  pages  VARCHAR(30)
);    

如何从 pages 列中搜索值 1。我已经确定了一些可能出现的情况,但无法为其创建解决方案。

如果我正在搜索 1,则以下模式应该是 handles

1,  match
11  shouldn't match
11, shouldn't match
,1, match
,1  match
1   match
21  shouldn't match
21, shouldn't match 

我一直在想这个问题,但是没有想出解决办法。我认为正常的 %LIKE% 不能在这里使用

样本 sql sqlfiddle

我还需要搜索多个值,例如 173

使用 FIND_IN_SET().

示例:

SELECT * FROM pages WHERE FIND_IN_SET('1', pages) 

来自文档:

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by “,” characters. If the first argument is a constant string and the second is a column of type SET, the FIND_IN_SET() function is optimized to use bit arithmetic. Returns 0 if str is not in strlist or if strlist is the empty string. Returns NULL if either argument is NULL. This function does not work properly if the first argument contains a comma (“,”) character.

(已添加突出显示)