SQL NOT 运算符

SQL NOT 运算符

NOT 是 Structured Query Language 中的逻辑运算符。

该运算符在 SQL 中否定任何布尔表达式的输出。

此运算符将给定的值与每个值进行比较,并返回不满足条件的那些值。

NOT 运算符主要与 INSERT、UPDATE、DELETE 和 UPDATE SQL 语句的 WHERE 子句一起使用。

Structured Query Language 中 NOT 运算符的语法:

SELECT First_Column_Name, Second_Column_Name, …., Nth_Column_Name FROM Table_Name WHERE NOT Condition;

在SQL语法中,我们必须在WHERE子句中使用NOT关键字指定条件。

在SQL中使用NOT运算符的示例

要理解Structured Query Language(结构化查询语言)中使用NOT运算符的查询,我们必须创建名为Worker_Info的新表。该表包含工作在工业界的工人的详细信息。

以下查询在Industry数据库中创建Worker_Info表:

CREATE TABLE Worker_Info
(
Worker_ID INT NOT NULL PRIMARY KEY,  
Worker_First_Name VARCHAR (100),  
Worker_Last_Name VARCHAR (100),  
Worker_Dept_Id INT NOT NULL,
Worker_Joining_Date Varchar (80),
Worker_City Varchar (80),
Worker_Salary INT 
);

以下是插入查询,插入在该行业工作的多个员工的记录:

INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Joining_Date, Worker_City, Worker_Salary) VALUES (5001, Arush, Sharma, 1001, 2020-01-02, Delhi, 20000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Joining_Date, Worker_City, Worker_Salary) VALUES (5002, Bulbul, Roy, 1002, 2019-12-31, Delhi, 38000 );
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Joining_Date, Worker_City, Worker_Salary) VALUES (5004, Saurabh, Sharma, 1001, 2020-10-10, Mumbai, 45000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Joining_Date, Worker_City, Worker_Salary) VALUES (5005, Shivani, Singhania, 1001, 2019-07-15, Kolkata, 42000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Joining_Date, Worker_City, Worker_Salary) VALUES (5006, Avinash, Sharma, 1002, 2019-11-11, Delhi, 28000);
INSERT INTO Worker_Info (Worker_ID, Worker_First_Name, Worker_Last_Name Worker_Dept_Id, Worker_Joining_Date, Worker_City, Worker_Salary)VALUES (5007, Shyam, Besas, 1003, 2021-06-21, Lucknow, 35000);

以下查询显示了Worker_Info表的数据。

SELECT * FROM Worker_Info;
Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5005 Shivani Singhania 1001 2019-07-15 Kolkata 42000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000

以下查询显示了上述表中不属于Kolkata City的工人的记录:

SELECT * FROM Worker_Info WHERE NOT Worker_City = 'Kolkata';
Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000

以下查询不显示表中工资大于30000的工人的记录。

SELECT * FROM Worker_Info WHERE NOT Worker_Salary > 30000;

输出:

Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000

NOT Operator with IN operator in SQL

我们还可以在SQL中使用NOT运算符与IN运算符一起过滤那些不在IN运算符括号中传递的记录。

NOT运算符与IN运算符的语法

SELECT First_Column_Name, Second_Column_Name, …., Nth_Column_Name FROM Table_Name WHERE Column_Name NOT IN (First_Value, Second_Value, Third_Value, Nth_Value);

NOT运算符与IN运算符的例子

以下查询显示了那些Worker_ID没有在IN运算符中传递的工人记录:

SELECT * FROM Worker_Info WHERE Worker_ID NOT IN (5004, 5005, 5007);

输出:

Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5001 Arush Sharma 1001 2020-01-02 Delhi 20000
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5006 Avinash Sharma 1002 2019-11-11 Delhi 28000

SQL中使用NOT运算符和LIKE运算符

我们在SQL中还可以使用NOT运算符和LIKE运算符来过滤不与指定模式匹配的值。

NOT运算符与LIKE运算符的语法

SELECT First_Column_Name, Second_Column_Name, …., Nth_Column_Name FROM Table_Name WHERE Column_Name NOT LIKE Pattern;

使用LIKE运算符的NOT运算符的示例

示例1: 以下查询显示了来自worker_Info表的那些名字不以A开头的工人记录。

SELECT * FROM Worker_Info WHERE Worker_First_Name NOT LIKE 'A%'

输出:

Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5002 Bulbul Roy 1002 2019-12-31 Delhi 38000
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5005 Shivani Singhania 1001 2019-07-15 Kolkata 42000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000

示例2: 下面的查询不会显示worker_Info表中City名称以D字母开头且以i字母结尾的工人记录。

SELECT Worker_Id, Worker_First_Name, Worker_City, Worker_Salary FROM Worker_Info WHERE Worker_City NOT LIKE 'D%i'

输出:

Worker_Id Worker_First_Name Worker_Last_Name Worker_Dept_Id Worker_Joining_Date Worker_City Worker_Salary
5004 Saurabh Roy 1001 2020-10-10 Mumbai 45000
5005 Shivani Singhania 1001 2019-07-15 Kolkata 42000
5007 Shyam Besas 1003 2021-06-21 Lucknow 35000

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程