SQL选择不包含Y但包含X的字符串

发布于 2024-12-08 11:42:13 字数 405 浏览 1 评论 0原文

我试图选择此数据库字段具有字符串但不包含特定值的所有实例。

示例:我试图选择“红色椅子”的每个实例,其中“红色椅子”后面没有“大”。如果我要在下表中运行该查询,我只会得到 ID 为 2 的行:

+----------+--------------+---------+
| ID       |        content         | 
+----------+------------------------+
| 1        | The big red chair      |
| 2        | I have a red chair     |
| 3        | I have a big chair     |
+----------+------------------------+

提前致谢!

I'm attempting to select all of the instances where this database field has a string but does not contain a specific value.

Example: I'm trying to select every instance of "red chair" where "red chair" is not proceeded by "big". If I were to run that query in the following table, I'd only get the row with the ID of 2 back:

+----------+--------------+---------+
| ID       |        content         | 
+----------+------------------------+
| 1        | The big red chair      |
| 2        | I have a red chair     |
| 3        | I have a big chair     |
+----------+------------------------+

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

王权女流氓 2024-12-15 11:42:14

您可以使用:

LIKE '%red chair%' AND NOT LIKE '%big%'

编辑:修复了 LIKE 匹配字符串

You can use:

LIKE '%red chair%' AND NOT LIKE '%big%'

edit: Fixed LIKE matching strings

会发光的星星闪亮亮i 2024-12-15 11:42:14

假设短语“red chair”在 content 中仅出现一次,这将得到您想要的结果。如果它可以出现多次(“红色椅子是一把大红色椅子”),你想要什么结果?

SELECT * FROM Furniture 
  WHERE content LIKE '%red chair%' AND content NOT LIKE '%big red chair%'

This will get what you want, assuming the phrase "red chair" occurs only once in content. If it can appear more than once ('The red chair is a big red chair'), what result do you want?

SELECT * FROM Furniture 
  WHERE content LIKE '%red chair%' AND content NOT LIKE '%big red chair%'
日记撕了你也走了 2024-12-15 11:42:14
WHERE content like '%red chair%'
AND content not like '%big red chair%'

但它不会很快!

WHERE content like '%red chair%'
AND content not like '%big red chair%'

It's not going to be fast though!

三月梨花 2024-12-15 11:42:14

选择以 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%';

Select string that STARTS WITH X and DOES NOT CONTAIN 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%';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文