gpt4 book ai didi

MYSQL REGEXP 查询关键字列

转载 作者:行者123 更新时间:2023-11-29 04:37:39 24 4
gpt4 key购买 nike

我需要一个正则表达式来获取只有关键字52的用户和有关键字52,53但没有54的用户。下面是表结构

CREATE TABLE `User` (
`id` int NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`keywords` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Sample record1: 100, Sam, Thompson, "50,51,52,53,54"
Sample record2: 100, Wan, Thompson, "50,52,53"
Sample record3: 100, Kan, Thompson, "53,52,50,54"

50 = sports
51 = cricket
52 = soccer
53 = baseball
54 = tennis

到目前为止,这是我提出的查询。它给出了所有 3 个记录。

SELECT * FROM `User` WHERE keywords REGEXP '[[:<:]]52,53,54[[:>:]]' 

最佳答案

尝试使用 FIND_IN_SET() 而不是复杂的正则表达式:

SELECT u.*
FROM User
WHERE text = '52' OR
(FIND_IN_SET('52', text) > 0 AND FIND_IN_SET('53', text) > 0 AND
FIND_IN_SET('54', text) = 0)

解释:

WHERE text = '52' -- users who have keyword 52 and only this keyword
WHERE FIND_IN_SET('52', text) > 0 AND FIND_IN_SET('53', text) > 0 AND
FIND_IN_SET('54', text) = 0
-- users who have keywords 52 and 53 but not 54

关于MYSQL REGEXP 查询关键字列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428015/

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