SQL选择不包含Y但包含X的字符串
我试图选择此数据库字段具有字符串但不包含特定值的所有实例。
示例:我试图选择“红色椅子”的每个实例,其中“红色椅子”后面没有“大”。如果我要在下表中运行该查询,我只会得到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用:
LIKE '%red chair%' AND NOT LIKE '%big%'
编辑:修复了 LIKE 匹配字符串
You can use:
LIKE '%red chair%' AND NOT LIKE '%big%'
edit: Fixed LIKE matching strings
假设短语“red chair”在
content
中仅出现一次,这将得到您想要的结果。如果它可以出现多次(“红色椅子是一把大红色椅子”),你想要什么结果?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?但它不会很快!
It's not going to be fast though!