SQL 如何使用IN关键词
在这篇SQL文章中,您将学习如何在SQL数据库的查询中使用IN关键词。
什么是SQL中的IN关键词?
IN 是结构化查询语言中的逻辑运算符,允许数据库用户在WHERE子句中定义多个值。
带有IN运算符的WHERE子句显示与给定一组值匹配的记录。我们还可以在IN运算符的括号中指定子查询。
我们可以在SQL数据库中的INSERT、SELECT、UPDATE和DELETE查询中使用IN运算符。
SQL中的IN运算符替代了查询中多个OR条件的过程。
IN运算符的语法:
SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name IN (Value_1, Value_2, Value_3, ......., Value_N);
如果您想在SQL语句中使用IN操作符,您必须按照以下步骤顺序进行:
- 在SQL中创建数据库。
- 创建新的SQL表。
- 将数据插入表中。
- 查看插入的数据。
- 使用SQL IN操作符显示表的数据。
现在,我们将逐步解释每个步骤,并提供最佳的SQL示例:
步骤1:创建一个简单的新数据库
第一步是在结构化查询语言中创建一个新的数据库。
以下CREATE语句在SQL服务器中创建了新的 Mechanical_College 数据库:
CREATE Database Mechanical_College;
步骤2:创建一个新表
现在,使用以下SQL语法来创建数据库中的新表:
CREATE TABLE table_name
(
1st_Column data type (character_size of 1st Column),
2nd_Column data type (character_size of the 2nd column ),
3rd_Column data type (character_size of the 3rd column),
...
Nth_Column data type (character_size of the Nth column)
);
以下CREATE语句在 机械学院 数据库中创建 Faculty_Info 表格:
CREATE TABLE Faculty_Info
(
Faculty_ID INT NOT NULL PRIMARY KEY,
Faculty_First_Name VARCHAR (100),
Faculty_Last_Name VARCHAR (100),
Faculty_Dept_Id INT NOT NULL,
Faculty_Joining_DateDATE,
Faculty_City Varchar (80),
Faculty_Salary INT
);
步骤3:将数据插入表格
以下的INSERT查询将Faculties的记录插入Faculty_Info表中:
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1001, Arush, Sharma, 4001, 2020-01-02, Delhi, 20000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1002, Bulbul, Roy, 4002, 2019-12-31, Delhi, 38000 );
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1004, Saurabh, Sharma, 4001, 2020-10-10, Mumbai, 45000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1005, Shivani, Singhania, 4001, 2019-07-15, Kolkata, 42000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary) VALUES (1006, Avinash, Sharma, 4002, 2019-11-11, Delhi, 28000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Joining_Date, Faculty_City, Faculty_Salary)VALUES (1007, Shyam, Besas, 4003, 2021-06-21, Lucknow, 35000);
步骤4:查看插入的数据
以下SELECT语句显示Faculty_Info表的数据:
SELECT * FROM Faculty_Info;
Faculty_Id | Faculty_First_Name | Faculty_Last_Name | Faculty_Dept_Id | Faculty_Joining_Date | Faculty_City | Faculty_Salary |
---|---|---|---|---|---|---|
1001 | Arush | Sharma | 4001 | 2020-01-02 | Delhi | 20000 |
1002 | Bulbul | Roy | 4002 | 2019-12-31 | Delhi | 38000 |
1004 | Saurabh | Roy | 4001 | 2020-10-10 | Mumbai | 45000 |
1005 | Shivani | Singhania | 4001 | 2019-07-15 | Kolkata | 42000 |
1006 | Avinash | Sharma | 4002 | 2019-11-11 | Delhi | 28000 |
1007 | Shyam | Besas | 4003 | 2021-06-21 | Lucknow | 35000 |
步骤5:使用IN运算符以不同的方式查看Faculty_Info表的数据
以下查询使用了带有IN运算符的数字值:
SELECT Faculty_Id, Faculty_First_Name, Faculty_Dept_Id, Faculty_Joining_Date, Faculty_Salary FROM Faculty_Info WHERE Faculty_Salary IN ( 38000, 42000, 45000, 35000);
这个查询仅显示工资在WHERE子句的IN运算符中传递的那些教职工的记录。
输出:
Faculty_Id | Faculty_First_Name | Faculty_Dept_Id | Faculty_Joining_Date | Faculty_Salary |
---|---|---|---|---|
1002 | Bulbul | 4002 | 2019-12-31 | 38000 |
1004 | Saurabh | 4001 | 2020-10-10 | 45000 |
1005 | Shivani | 4001 | 2019-07-15 | 42000 |
1007 | Shyam | 4003 | 2021-06-21 | 35000 |
以下查询使用了带有 IN 逻辑运算符的文本或字符值:
SELECT Faculty_Id, Faculty_First_Name, Faculty_Joining_Date, Faculty_City FROM Faculty_Info WHERE Faculty_City IN ( Mumbai, Kolkata, Lucknow);
这个查询仅显示那些城市包含在WHERE子句中IN操作符的括号中的Faculties的记录。
输出:
Faculty_Id | Faculty_First_Name | Faculty_Joining_Date | Faculty_City |
---|---|---|---|
1004 | Saurabh | 2020-10-10 | Mumbai |
1005 | Shivani | 2019-07-15 | Kolkata |
1007 | Shyam | 2021-06-21 | Lucknow |
以下查询使用带有IN逻辑运算符的DATEformat:
SELECT Faculty_Id, Faculty_First_Name, Faculty_Dept_ID Faculty_Joining_Date, Faculty_Salary FROM Faculty_Info WHERE Faculty_Joining_Date IN (2020-01-02, 2021-06-21, 2020-10-10, 2019-07-15);
该查询仅显示加入日期在WHERE子句的IN运算符中传递的Faculties记录。
输出:
Faculty_Id | Faculty_First_Name | Faculty_Dept_Id | Faculty_Joining_Date | Faculty_Salary |
---|---|---|---|---|
1001 | Arush | 4001 | 2020-01-02 | 20000 |
1004 | Saurabh | 4001 | 2020-10-10 | 45000 |
1005 | Shivani | 4001 | 2019-07-15 | 42000 |
1007 | Shyam | 4003 | 2021-06-21 | 35000 |
以下查询使用了带有IN逻辑运算符的SQL UPDATE命令:
UPDATE Faculty_Info SET Faculty_Salary = 50000 WHERE Faculty_Dept_ID IN (4002, 4003);
这个查询更新那些在WHERE子句的IN运算符中传递了Dept_Id的Faculty的工资。
要检查上述查询的结果,请在SQL中输入以下SELECT查询:
SELECT * FROM Faculty_Info;
Faculty_Id | Faculty_First_Name | Faculty_Last_Name | Faculty_Dept_Id | Faculty_Joining_Date | Faculty_City | Faculty_Salary |
---|---|---|---|---|---|---|
1001 | Arush | Sharma | 4001 | 2020-01-02 | Delhi | 20000 |
1002 | Bulbul | Roy | 4002 | 2019-12-31 | Delhi | 50000 |
1004 | Saurabh | Roy | 4001 | 2020-10-10 | Mumbai | 45000 |
1005 | Shivani | Singhania | 4001 | 2019-07-15 | Kolkata | 42000 |
1006 | Avinash | Sharma | 4002 | 2019-11-11 | Delhi | 50000 |
1007 | Shyam | Besas | 4003 | 2021-06-21 | Lucknow | 50000 |
SQL IN运算符与子查询
在结构化查询语言中,我们也可以使用子查询与IN逻辑运算符。
IN运算符与子查询的语法如下所示:
SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name IN (Subquery);
如果要理解带有子查询的IN运算符,您必须使用CREATE语句在结构化查询语言中创建两个不同的表。
以下查询将在数据库中创建Faculty_Info表:
CREATE TABLE Faculty_Info
(
Faculty_ID INT NOT NULL PRIMARY KEY,
Faculty_First_Name VARCHAR (100),
Faculty_Last_Name VARCHAR (100),
Faculty_Dept_Id INT NOT NULL,
Faculty_Address Varchar (80),
Faculty_City Varchar (80),
Faculty_Salary INT
);
以下查询在数据库中创建了 Department_Info 表:
CREATE TABLE Department_Info
(
Dept_Id INT NOT NULL,
Dept_Name Varchar(100),
Head_Id INT
);
以下INSERT查询将Faculties表中的记录插入到Faculty_Info表中:
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1001, Arush, Sharma, 4001, 22 street, New Delhi, 20000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1002, Bulbul, Roy, 4002, 120 street, New Delhi, 38000 );
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1004, Saurabh, Sharma, 4001, 221 street, Mumbai, 45000);
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_NameFaculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1005, Shivani, Singhania, 4001, 501 street, Kolkata, 42000);
以下INSERT查询将部门记录插入Department_Info表中:
INSERT INTO Department_Info (Dept_ID, Dept_Name, Head_Id) VALUES ( 4001, Arun, 1005);
INSERT INTO Department_Info (Dept_ID, Dept_Name, Head_Id) VALUES ( 4002, Zayant, 1009);
INSERT INTO Department_Info (Dept_ID, Dept_Name, Head_Id) VALUES ( 4003,
Manish, 1007);
以下是显示Faculty_Info表数据的SELECT语句:
SELECT * FROM Faculty_Info;
Faculty_Id | Faculty_First_Name | Faculty_Last_Name | Faculty_Dept_Id | Faculty_Address | Faculty_City | Faculty_Salary |
---|---|---|---|---|---|---|
1001 | Arush | Sharma | 4001 | 22 Street | New Delhi | 20000 |
1002 | Bulbul | Roy | 4002 | 120 Street | New Delhi | 38000 |
1004 | Saurabh | Roy | 4001 | 221 Street | Mumbai | 45000 |
1005 | Shivani | Singhania | 4001 | 501 Street | Kolkata | 42000 |
1006 | Avinash | Sharma | 4002 | 12 Street | Delhi | 28000 |
1007 | Shyam | Besas | 4003 | 202 Street | Lucknow | 35000 |
以下查询显示了Department_Info表中的部门记录:
SELECT * FROM Department_Info;
以下查询使用了IN运算符和一个子查询:
SELECT * FROM Faculty_Info WHERE Faculty_Dept_Id IN ( Select Dept_Id FROM Department_Info WHERE Head_Id >= 1007);
此查询显示Faculty_Info表的Dept_ID与Department_Info表的Dept_ID匹配的教职工的记录。
输出:
Faculty_Id | Faculty_First_Name | Faculty_Last_Name | Faculty_Dept_Id | Faculty_Address | Faculty_City | Faculty_Salary |
---|---|---|---|---|---|---|
1002 | Bulbul | Roy | 4002 | 120 Street | New Delhi | 38000 |
1006 | Avinash | Sharma | 4002 | 12 Street | Delhi | 28000 |
1007 | Shyam | Besas | 4003 | 202 Street | Lucknow | 35000 |
什么是SQL中的NOT IN操作符?
NOT IN是Structured Query Language中的另一个操作符,它与SQL中的IN操作符正好相反。它允许您访问表中未通过IN操作符括号中的值。
NOT IN操作符可在INSERT、UPDATE、SELECT和DELETE SQL查询中使用。
NOT IN操作符的语法:
SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name NOT IN (Value_1, Value_2, Value_3, ......., Value_N);
如果你想在SQL语句中使用NOT IN运算符,你必须按照给定的步骤依次操作:
- 创建一个数据库在SQL系统中。
- 在数据库中创建新表。
- 向表中插入数据。
- 查看已插入的数据。
- 使用NOT IN运算符查看数据。
现在,我们将逐一简要解释每一步,并提供最佳的SQL示例:
步骤1:创建一个简单的新数据库
以下查询在SQL服务器中创建了新的 Civil_Industry 数据库:
CREATE Database Industry;
步骤2:创建新表
以下查询在 Civil_Industry 数据库中创建 Worker_Info 表:
CREATE TABLE Worker_Info
(
Worker_ID INT NOT NULL PRIMARY KEY,
Worker_Name VARCHAR (100),
Worker_Gender Varchar(20),
Worker_Age INT NOT NULL DEFAULT 18,
Worker_Address Varchar (80),
Worker_Salary INT NOT NULL
);
步骤 3: 插入数值
以下是插入 Worker_Info 表中工人记录的 INSERT 查询语句:
INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1001, Arush, Male, Agra, 35000);
INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1002, Bulbul, Female, Lucknow, 42000);
INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1004, Saurabh, Male, 20, Lucknow, 45000);
INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1005, Shivani, Female, Agra, 28000);
INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1006, Avinash, Male, 22, Delhi, 38000);
INSERT INTO Worker_Info (Worker_ID, Worker_Name, Worker_Gender, Worker_Age, Worker_Address, Worker_Salary) VALUES (1007, Shyam, Male, Banglore, 20000);
步骤4:查看表的数据
下面的查询显示了Worker_Info表的数据。
SELECT * FROM Worker_Info;
Worker_ID | Worker_Name | Worker_Gender | Worker_Age | Worker_Address | Worker_Saalary |
---|---|---|---|---|---|
1001 | Arush | Male | 18 | Agra | 35000 |
1002 | Bulbul | Female | 18 | Lucknow | 42000 |
1004 | Saurabh | Male | 20 | Lucknow | 45000 |
1005 | Shivani | Female | 18 | Agra | 28000 |
1006 | Avinash | Male | 22 | Delhi | 38000 |
1007 | Shyam | Male | 18 | Banglore | 20000 |
步骤5:使用NOT IN运算符
下面的查询使用NOT IN运算符与数值数据:
SELECT * FROM Worker_Info WHERE Worker_salary NOT IN (35000, 28000, 38000);
此SELECT查询显示输出中工资未包含在NOT IN运算符中的所有工人。
上述语句的结果如下表所示:
Worker_ID | Worker_Name | Worker_Gender | Worker_Age | Worker_Address | Worker_Saalary |
---|---|---|---|---|---|
1002 | Bulbul | Female | 18 | Lucknow | 42000 |
1004 | Saurabh | Male | 20 | Lucknow | 45000 |
1007 | Shyam | Male | 18 | Banglore | 20000 |
以下查询使用NOT IN逻辑运算符和字符或文本值:
SELECT * FROM Worker_Info WHERE Worker_Address NOT IN (Lucknow, Delhi);
这个查询显示了所有那些地址没有在NOT IN操作符中传递的工作者的记录。
输出:
Worker_ID | Worker_Name | Worker_Gender | Worker_Age | Worker_Address | Worker_Saalary |
---|---|---|---|---|---|
1001 | Arush | Male | 18 | Agra | 35000 |
1005 | Shivani | Female | 18 | Agra | 28000 |
1007 | Shyam | Male | 18 | Banglore | 20000 |