我正在尝试选择此数据库字段包含字符串但不包含特定值的所有实例。
示例:我正在尝试选择“红色椅子”的每个实例,其中“红色椅子”不是“大”。如果我在下表中运行该查询,我只会得到ID为2的行:
+----------+--------------+---------+ | ID | content | +----------+------------------------+ | 1 | The big red chair | | 2 | I have a red chair | | 3 | I have a big chair | +----------+------------------------+
提前致谢!
答案 0 :(得分:15)
您可以使用:
LIKE '%red chair%' AND NOT LIKE '%big%'
修改:修复了LIKE匹配字符串
答案 1 :(得分:4)
假设短语“red chair”仅在content
中出现一次,这将得到您想要的结果。如果它可以出现不止一次('红色椅子是一把大红色椅子'),你想要什么结果?
SELECT * FROM Furniture
WHERE content LIKE '%red chair%' AND content NOT LIKE '%big red chair%'
答案 2 :(得分:3)
WHERE content like '%red chair%'
AND content not like '%big red chair%'
虽然不会很快!
答案 3 :(得分:0)
选择以X开头并且不包含X的字符串
WITH T
AS
(
SELECT *
FROM (
VALUES ('xenophilia'),
('xbox'),
('regex')
) AS T (c)
)
SELECT *
FROM T
WHERE c LIKE 'x%'
AND c NOT LIKE '_%x%';