gpt4 book ai didi

mysql - 在逗号分隔的 mysql 列上搜索

转载 作者:行者123 更新时间:2023-11-29 01:55:07 25 4
gpt4 key购买 nike

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

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

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

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

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%

sqlfiddle 上的示例 sql

我还需要搜索多个值,如 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.

(已添加突出显示)

关于mysql - 在逗号分隔的 mysql 列上搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com