Sql 简明教程
SQL - NOT Operator
大多数情况下,需要使用两个或多个条件从表中筛选所需的记录;但有时满足其中一个条件就足够了。在某些情况下,您还需要检索不满足指定条件的记录。SQL 为此提供了 logical connectives 。它们如下所示−
Most of the times, there is a need to use two or more conditions to filter required records from a table; but sometimes satisfying one of the conditions would be enough. There are also scenarios when you need to retrieve records that do not satisfy the conditions specified. SQL provides logical connectives for this purpose. They are listed below −
-
AND − Operator
-
OR − Operator
-
NOT − Operator
借助这些逻辑连接符,可以检索所需的记录,还可以为不需要检索的记录创建异常。
With the help of these logical connectives, one can retrieve records that are required and also create exceptions for the records that are not needed to be retrieved.
The SQL NOT Operator
SQL NOT 是一个逻辑运算符/连接符,用于在 WHERE 子句中否定条件或布尔表达式。也就是说,TRUE 变为 FALSE,反之亦然。
SQL NOT is a logical operator/connective used to negate a condition or Boolean expression in a WHERE clause. That is, TRUE becomes FALSE and vice versa.
可以使用此运算符的最常见场景是,当指定结果表中不包含什么内容而不是包含什么内容时。
The most common scenario where this operator can be used occurs when there is a specification of what NOT to include in the result table, instead of what to include.
例如,在印度投票系统中,18 岁以下的人员不得投票。因此,在检索所有有资格投票的人员的信息时,可以使用 NOT 运算符为未成年人创建异常,因为这是唯一规范。
For instance, in an Indian voting system, people younger than 18 years of age are NOT allowed to vote. Therefore, while retrieving the information of all people who are eligible to vote, using the NOT operator, we can create an exception to minors since it is the only specification.
Syntax
以下是要 SQL NOT 运算符的语法 −
Following is the syntax for SQL NOT operator −
NOT [CONDITION or BOOLEAN EXPRESSION];
Example
在下面的示例中,我们首先创建一个表来说明如何使用 NOT 运算符。
In the following example, let us first create a table to demonstrate the usage of NOT operator.
使用下面的查询,我们创建一个名为 CUSTOMERS 的表,其中包含客户的个人详细信息,包括姓名、年龄、地址和薪水等。−
Using the query below, we are creating a table named CUSTOMERS, which contains the personal details of customers including their name, age, address and salary etc. −
CREATE TABLE CUSTOMERS (
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25),
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
现在,使用如下所示的 INSERT 语句向此表中插入值:
Now, insert values into this table using the INSERT statement as follows −
INSERT INTO CUSTOMERS VALUES
(1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ),
(2, 'Khilan', 25, 'Delhi', 1500.00 ),
(3, 'Kaushik', 23, 'Kota', 2000.00 ),
(4, 'Chaitali', 25, 'Mumbai', 6500.00 ),
(5, 'Hardik', 27, 'Bhopal', 8500.00 ),
(6, 'Komal', 22, 'Hyderabad', 4500.00 ),
(7, 'Muffy', 24, 'Indore', 10000.00 );
该表将按如下方式创建:
The table will be created as follows −
ID |
NAME |
AGE |
ADDRESS |
SALARY |
1 |
Ramesh |
32 |
Ahmedabad |
2000.00 |
2 |
Khilan |
25 |
Delhi |
1500.00 |
3 |
Kaushik |
23 |
Kota |
2000.00 |
4 |
Chaitali |
25 |
Mumbai |
6500.00 |
5 |
Hardik |
27 |
Bhopal |
8500.00 |
6 |
Komal |
22 |
Hyderabad |
4500.00 |
7 |
Muffy |
24 |
Indore |
10000.00 |
下面的 SQL 查询检索 'CUSTOMERS' 表中 'SALARY' 列不大于 2000.00 的所有行 −
The SQL query below retrieves all rows from the 'CUSTOMERS' table where the 'SALARY' column is not greater than 2000.00 −
SELECT * FROM CUSTOMERS WHERE NOT (SALARY > 2000.00);
SQL NOT Operator with LIKE
LIKE 运算符使用通配符在提取匹配记录之前对表记录执行模式匹配。
The LIKE operator uses wildcards to perform pattern matching on the records of a table before extracting the matched records.
但是,要否定此操作(而是提取不匹配的记录),我们可以使用 NOT 运算符和 NOT LIKE 关键字形式的 LIKE。
However, to negate this operation (to extract the unmatched records instead), we can use the NOT operator along with LIKE in the form of NOT LIKE keyword.
SQL NOT Operator with IN
IN 运算符返回 TRUE,如果表列中的值属于 WHERE 子句中指定的一系列数字。
The IN operator returns TRUE if the values in a table column belong to a range of numbers specified in the WHERE clause.
为了否定此操作,我们可以改用 NOT IN 运算符。这样,如果记录不在给定范围内,那么布尔表达式将返回 TRUE。
To negate this operation, we can use the NOT IN operator instead. With this, the Boolean expression returns TRUE if the records are not present in the given range.
SQL NOT Operator with IS NULL
IS NULL 运算符用于检查表中的记录是否为 NULL。如果遇到 NULL 值,则返回 TRUE;否则返回 FALSE。
The IS NULL operator is used to check whether the records in a table are NULL. If a NULL value is encountered, it returns TRUE; and FALSE otherwise.
通过将 NOT 运算符与 IS NULL 运算符一起使用,我们可以提取不包含 NULL 值的所有记录。
Using NOT operator with the IS NULL operator, we can extract all the records that does not contain NULL values.
Example
此 SQL 查询检索“CUSTOMERS”表中“AGE”列不为 null 的所有行,即包含有效年龄值的行:
This SQL query retrieves all rows from the 'CUSTOMERS' table where the 'AGE' column is not null, i.e. it contains valid age values −
SELECT * FROM CUSTOMERS WHERE AGE IS NOT NULL;
Output
结果表与原始表完全相同,因为它不包含任何 NULL 值:
The result table is exactly as the original table as it contains no NULL values −
ID |
NAME |
AGE |
ADDRESS |
SALARY |
1 |
Ramesh |
32 |
Ahmedabad |
2000.00 |
2 |
Khilan |
25 |
Delhi |
1500.00 |
3 |
Kaushik |
23 |
Kota |
2000.00 |
4 |
Chaitali |
25 |
Mumbai |
6500.00 |
5 |
Hardik |
27 |
Bhopal |
8500.00 |
6 |
Komal |
22 |
Hyderabad |
4500.00 |
7 |
Muffy |
24 |
Indore |
10000.00 |
但是,如果表包含任何 NULL 值,那么包含这些值的那些行将在结果表中被忽略。
However, if the table contains any NULL values, the rows containing it will be omitted in the resultant table.
SQL NOT Operator with BETWEEN
BETWEEN 运算符用于将范围建立为条件。当与 WHERE 子句一起使用时,此运算符的作用类似于布尔表达式。也就是说,如果表列的值位于指定范围内,则返回 TRUE;否则返回 FALSE。
BETWEEN operator is used to establish a range as a condition. When used with WHERE clause, this operator acts like a Boolean expression. That is, if values of a table column fall in the specified range, TRUE is returned; and FALSE otherwise.
使用带有 WHERE 子句的 NOT BETWEEN 运算符将返回其否定值。也就是说,如果表列的值位于指定范围内,则返回 FALSE;否则返回 TRUE。
Using NOT BETWEEN operator with WHERE clause will return its negation. That is, if values of a table column fall in the specified range, FALSE is returned; and TRUE otherwise.
SQL NOT Operator with EXISTS
EXISTS 运算符的工作方式类似于 IN 运算符;它将表记录与 WHERE 子句中指定的范围进行比较。但是,IN 运算符无法将 NULL 记录与范围进行比较,而 EXISTS 则可以。
The EXISTS operator works similar to the IN operator; it compares the table records with the specified range in the WHERE clause. However, the IN operator cannot compare the NULL records with the range while EXISTS does.
NOT EXISTS 运算符用于否定此操作。
The NOT EXISTS operator is used to negate this operation.
Example
在下面的示例中,让我们创建一个另一个表 Orders 以帮助演示与 EXISTS 运算符配合使用 NOT 运算符:
In the following example, let us create another table Orders to help in demonstrating the usage of NOT operator with EXISTS operator −
CREATE TABLE ORDERS (
OID INT NOT NULL,
DATE VARCHAR (20) NOT NULL,
CUSTOMER_ID INT NOT NULL,
AMOUNT DECIMAL (18, 2)
);
使用 INSERT 语句像下面这样向该表中插入值:
Using the INSERT statement, insert values into this table as follows −
INSERT INTO ORDERS VALUES
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);
该表显示如下:
The table is displayed as follows −
OID |
DATE |
CUSTOMER_ID |
AMOUNT |
102 |
2009-10-08 00:00:00 |
3 |
3000.00 |
100 |
2009-10-08 00:00:00 |
3 |
1500.00 |
101 |
2009-11-20 00:00:00 |
2 |
1560.00 |
103 |
2008-05-20 00:00:00 |
4 |
2060.00 |
下面的查询用于打印 CUSTOMERS 表中不存在于 ORDERS 表中的那些客户的 ID:
Following query is used to print the IDs of customers in CUSTOMERS table that do not exist in the ORDERS table −
SELECT * FROM CUSTOMERS WHERE NOT EXISTS (
SELECT CUSTOMER_ID FROM ORDERS
WHERE ORDERS.CUSTOMER_ID = CUSTOMERS.ID);