SQL查询其中字段不包含$ x [英] SQL Query Where Field DOES NOT Contain $x

查看:563
本文介绍了SQL查询其中字段不包含$ x的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一个SQL查询来查找其中field1不包含$ x的行.我该怎么办?

I want to find an SQL query to find rows where field1 does not contain $x. How can I do this?

推荐答案

这是哪种字段? IN运算符不能与单个字段一起使用,而只能在子查询或预定义列表中使用:

What kind of field is this? The IN operator cannot be used with a single field, but is meant to be used in subqueries or with predefined lists:

-- subquery
SELECT a FROM x WHERE x.b NOT IN (SELECT b FROM y);
-- predefined list
SELECT a FROM x WHERE x.b NOT IN (1, 2, 3, 6);

如果要搜索字符串,请使用LIKE运算符(但这会很慢):

If you are searching a string, go for the LIKE operator (but this will be slow):

-- Finds all rows where a does not contain "text"
SELECT * FROM x WHERE x.a NOT LIKE '%text%';

如果将其限制为要搜索的字符串必须以给定的字符串开头,则可以使用索引(如果该字段上有索引)并且速度相当快:

If you restrict it so that the string you are searching for has to start with the given string, it can use indices (if there is an index on that field) and be reasonably fast:

-- Finds all rows where a does not start with "text"
SELECT * FROM x WHERE x.a NOT LIKE 'text%';

这篇关于SQL查询其中字段不包含$ x的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆