SQL 如何使用BETWEEN

SQL 如何使用BETWEEN

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

BETWEEN在SQL中是什么?

BETWEEN关键字是结构化查询语言中的一个运算符。它允许数据库用户访问指定范围内的值。

我们可以在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运算符,您必须按照以下步骤依次执行:

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

现在,我们将逐步简短地解释每个步骤,并提供最佳的SQL示例:

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

首先,您必须在结构化查询语言数据库中创建一个新数据库。以下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查询中使用了DISTINCT子句和BETWEEN运算符:

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

SQL 如何使用BETWEEN

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

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

此UPDATE查询更新了那些入职日期在2019-07-15和2021-01-02之间的教职员工的工资。

要检查以上查询的结果,请键入以下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查询中使用。

结构化查询语言中使用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运算符,您必须按照以下给定的步骤依次执行:

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

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

首先,在结构化查询语言中创建一个新的数据库。

以下查询在SQL服务器中创建了新的 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
);

第2步:插入数值

以下的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

第5步:使用NOT BETWEEN运算符

下面的语句使用了NOT BETWEEN运算符和数字数据:

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

这个SELECT查询显示输出中工资不大于或等于25000且小于或等于40000的所有工人。

以上查询的结果如下所示:

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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程