如何在 SQL 中使用 BETWEEN

一、SQL BETWEEN 语法

在这篇 SQL 文章中,您将学习如何在数据库的 SQL 语句中使用 BETWEEN 关键字。在这里,我们还将详细讨论 NOT BETWEEN 运算符。

BETWEEN 关键字是SQL语言中的运算符。它允许数据库用户访问指定范围内的值。

我们可以轻松地在 INSERT、UPDATE、SELECT 和 DELETE SQL 查询的 WHERE 子句中使用 BETWEEN 运算符。

当列值小于或等于高值且大于或等于低值时,此运算符的计算结果为 TRUE。开始值和结束值包含在 BETWEEN 运算符中。

BETWEEN 逻辑运算符的语法:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name BETWEEN Value_1 AND Value_2;

在语法中,Value_1 指定范围的开始值,Value_2 指定范围的结束值。

二、SQL BETWEEN 使用步骤

如果要在 SQL 语句中使用 BETWEEN 运算符,则必须按相同顺序执行以下步骤:

  1. 在系统中创建一个数据库。
  2. 创建新的 SQL 表。
  3. 在表中插入数据。
  4. 查看插入的数据。
  5. 使用 BETWEEN 运算符可以多种方式查看表的数据。

现在,我们将通过最好的 SQL 示例,对每一步进行简要说明:

第 1 步:创建一个简单的新数据库

首先,您必须在SQL语言数据库中创建一个新数据库。以下 CREATE 语句创建新的IIT_College数据库:

CREATE Database IIT_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 语句在IIT_College数据库中创建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_Date Varchar (80),  
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 步:使用 BETWEEN 运算符

以下查询对数字数据使用 BETWEEN 运算符:

SELECT * FROM Faculty_Info WHERE Faculty_salary BETWEEN 25000 AND 40000;  

此 SELECT 查询显示输出中薪水介于 25000 和 40000 之间的所有学院。

上述语句的结果显示在以下输出中:

Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Joining_Date Faculty_City Faculty_Salary
1002 Bulbul Roy 4002 2019-12-31 Delhi 38000
1006 Avinash Sharma 4002 2019-11-11 Delhi 28000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

以下查询使用带有字符或文本值的 BETWEEN 逻辑运算符:

SELECT * FROM Faculty_Info WHERE Faculty_City BETWEEN D AND L; 

此查询显示所有城市名称以 D 和 L 之间的任何字母开头的学院的记录。

Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Joining_Date Faculty_City Faculty_Salary
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

以下查询使用日期格式的 BETWEEN 逻辑运算符

SELECT * FROM Faculty_Info WHERE Faculty_Joining_Date BETWEEN 2020-01-02 AND 2021-06-21;  

此查询显示所有在 2020-01-02 和 2021-06-21 之间加入的院系的记录。

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
1004 Saurabh Roy 4001 2020-10-10 Mumbai 45000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

以下查询在 SELECT 查询中使用带有 BETWEEN 运算符的 DISTINCT 子句:

SELECT DISTINCT (Faculty_City) FROM Faculty_Info WHERE Faculty_Dept_Id BETWEEN 4001 AND 4003;  

以下查询使用 UPDATE 命令中的 BETWEEN 运算符更新表的数据:

UPDATE Faculty_Info SET Faculty_Salary = 60000 WHERE Faculty_Joining_Date BETWEEN 2019-07-15 AND 2021-01-02;  

此 UPDATE 查询会更新加入日期介于

2019 年 7 月 15 日和 2021 年 1 月 2 日。

要检查上述查询的结果,请键入以下 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 60000
1002 Bulbul Roy 4002 2019-12-31 Delhi 60000
1004 Saurabh Roy 4001 2020-10-10 Mumbai 45000
1005 Shivani Singhania 4001 2019-07-15 Kolkata 60000
1006 Avinash Sharma 4002 2019-11-11 Delhi 60000
1007 Shyam Besas 4003 2021-06-21 Lucknow 35000

三、SQL NOT BETWEEN  语法

NOT BETWEEN 是SQL语言中的运算符,与 BETWEEN 运算符正好相反。它允许我们从表中访问那些不在给定范围内的值。

NOT BETWEEN 运算符可用于 INSERT、UPDATE、SELECT 和 DELETE SQL 查询。

在SQL语言中使用 NOT BETWEEN 运算符的语法如下:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name NOT BETWEEN Value_1 AND Value_2; 

四、SQL NOT BETWEEN  使用步骤

如果要在 SQL 语句中使用 NOT BETWEEN 运算符,则必须以相同的顺序一一执行以下给定步骤:

  1. 在系统中创建一个数据库。
  2. 创建新的 SQL 表。
  3. 在表中插入数据。
  4. 查看插入的数据。
  5. 使用 NOT BETWEEN 运算符查看不同列的具体数据。

第 1 步:创建简单的新数据库

首先,您必须使用结构化查询语言创建一个新数据库。

以下查询在 SQL Server 中创建新的Civil_Industry数据库:

CREATE Database Civil_Industry;  

第 2 步:创建新表

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

CREATE TABLE Worker_Info  
(  
Worker_ID INT NOT NULL,    
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 步:插入值

以下 INSERT 查询在 Worker_Info 表中插入工人的记录:

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

五、使用 NOT BETWEEN 运算符 示例

以下语句将 NOT BETWEEN 运算符与数字数据一起使用:

SELECT * FROM Worker_Info WHERE Worker_salary NOT BETWEEN 25000 AND 40000;  

 此 SELECT 查询显示输出中薪水不大于或等于 25000 小于或等于 40000 的所有 Worker。

上述查询的结果显示在以下输出中:

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 BETWEEN 逻辑运算符与字符或文本值:

SELECT * FROM Worker_Info WHERE Worker_Address NOT BETWEEN D AND L;  

 此查询显示所有城市名称不以 D 和 L 之间的任何字母开头的工人的记录。

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

热门文章

优秀文章