SQL 如何使用Any关键字
在这篇SQL文章中,您将学习如何在数据库表中使用“ANY”关键字。
什么是SQL中的Any?
ANY是SQL中的一个运算符。该运算符将给定的值与每个子查询的值进行比较,并返回满足条件的那些值。
ANY运算符主要与INSERT、UPDATE、DELETE和UPDATE SQL语句中的HAVING或WHERE子句一起使用。
如果至少有一个子查询的值根据给定条件匹配,它将始终计算为TRUE。
使用ANY运算符的结构化查询语言的语法:
SELECT Column_Name_1, Column_Name_2, Column_Name_3, ......, Column_Name_N FROM Table_Name WHERE Column_Name Comparison_Operator ANY (SELECT Column_Name FROM Table_Name WHERE [condition]);
在语法中,ANY运算符后跟SQL比较运算符,用于将列值与子查询进行比较。
以下是在查询中与ANY运算符一起使用的SQL比较运算符:
1. 等于运算符(=)
与ANY运算符一起使用的等于比较运算符在列值等于子查询的任何值时返回TRUE。
语法:
Column_Name = ANY (subquery);
2. 不等于运算符(!=)
这个与任何运算符结合使用的比较运算符在列的值不等于子查询的任何值时,返回TRUE。
语法:
Column_Name != ANY (subquery);
**3. 大于运算符( >) **
此与任何运算符的比较运算符在列的值大于子查询的最小值时评估为TRUE。
语法:
Column_Name > ANY (subquery);
**4. 小于运算符 ( <) **
这个与任何运算符一起使用的比较运算符在列的值小于子查询的最大值时返回 TRUE。
语法:
Column_Name < ANY (subquery);
**5. 大于等于运算符 ( >=) **
此比较运算符与任何运算符结合使用时,在列的值大于或等于子查询的最小值时,返回TRUE。
语法:
Column_Name >= ANY (subquery);
**6. 小于等于运算符 ( <=) **
当该列的值小于等于子查询的最大值时,此比较运算符与任何运算符一起评估为TRUE。
语法:
Column_Name <= ANY (subquery);
如果您想在表中使用SQL ANY运算符进行操作,您必须按照以下步骤进行操作:
- 在系统中创建一个数据库。
- 创建两个新表。
- 向两个表中插入数据。
- 查看两个表的已插入数据。
- 使用ANY运算符以不同的方式查看数据。
现在,我们将用一个SQL示例详细解释这些步骤:
第1步:创建一个简单的新数据库
首先,您需要用结构化查询语言创建一个新数据库。所以,让我们开始吧。
以下查询在SQL服务器中创建了一个名为 College 的新数据库:
CREATE Database College;
第2步:创建新表
现在,使用下面的SQL语法,在数据库中创建新表:
CREATE TABLE table_name
(
column_Name_1 data type (character_size of the column_1),
column_Name_2 data type (character_size of the column_2),
column_Name_3 data type (character_size of the column_3),
...
column_Name_N data type (character_size of the column_N)
);
以下查询在 College 数据库中创建 Teacher_Info 表:
CREATE TABLE Teacher_Info
(
Teacher_ID INT NOT NULL PRIMARY KEY,
Teacher_First_Name VARCHAR (100),
Teacher_Last_Name VARCHAR (100),
Teacher_Dept_Id INT NOT NULL,
Teacher_Address Varchar (80),
Teacher_City Varchar (80),
Teacher_Salary INT
);
下面的查询在 College 数据库中创建了 Department_Info 表:
CREATE TABLE Department_Info
(
Dept_Id INT NOT NULL,
Dept_Name Varchar(100),
Head_Id INT
);
第3步:插入数据
以下INSERT查询将教师记录插入Teacher_Info表中:
INSERT INTO Teacher_Info (Teacher_ID, Teacher_First_Name, Teacher_Last_NameTeacher_Dept_Id, Teacher_Address, Teacher_City, Teacher_Salary) VALUES (1001, Arush, Sharma, 4001, 22 street, New Delhi, 20000);
INSERT INTO Teacher_Info (Teacher_ID, Teacher_First_Name, Teacher_Last_NameTeacher_Dept_Id, Teacher_Address, Teacher_City, Teacher_Salary) VALUES (1002, Bulbul, Roy, 4002, 120 street, New Delhi, 38000 );
INSERT INTO Teacher_Info (Teacher_ID, Teacher_First_Name, Teacher_Last_NameTeacher_Dept_Id, Teacher_Address, Teacher_City, Teacher_Salary) VALUES (1004, Saurabh, Sharma, 4001, 221 street, Mumbai, 45000);
INSERT INTO Teacher_Info (Teacher_ID, Teacher_First_Name, Teacher_Last_NameTeacher_Dept_Id, Teacher_Address, Teacher_City, Teacher_Salary) VALUES (1005, Shivani, Singhania, 4001, 501 street, Kolkata, 42000);
INSERT INTO Teacher_Info (Teacher_ID, Teacher_First_Name, Teacher_Last_NameTeacher_Dept_Id, Teacher_Address, Teacher_City, Teacher_Salary) VALUES (1006, Avinash, Sharma, 4002, 12 street, Delhi, 28000);
INSERT INTO Teacher_Info (Teacher_ID, Teacher_First_Name, Teacher_Last_NameTeacher_Dept_Id, Teacher_Address, Teacher_City, Teacher_Salary)VALUES (1007, Shyam, Besas, 4003, 202 street, Lucknow, 35000);
以下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);
第4步:查看数据表中的数据
以下查询显示了 Teacher_Info 表的数据。
SELECT * FROM Teacher_Info;
Teacher_Id | Teacher_First_Name | Teacher_Last_Name | Teacher_Dept_Id | Teacher_Address | Teacher_City | Teacher_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 |
以下查询显示了Teacher_Info表的数据。
SELECT * FROM Department_Info;
第5步:使用任何运算符查看表的数据
以下查询使用等号比较运算符和任何运算符:
SELECT * FROM Teacher_Info WHERE Teacher_Id = ANY (SELECT Head_Id from Department_Info);
该查询显示了来自“Teacher_Info”表中教师的详细信息。在这里,教师还兼任来自“Department_Info”表的部门负责人。
上述使用等号操作符的SELECT查询的输出如下表所示:
Teacher_Id | Teacher_First_Name | Teacher_Last_Name | Teacher_Dept_Id | Teacher_Address | Teacher_City | Teacher_Salary |
---|---|---|---|---|---|---|
1005 | Shivani | Singhania | 4001 | 501 Street | Kolkata | 42000 |
1007 | Shyam | Besas | 4003 | 202 Street | Lucknow | 35000 |
下面的查询使用ANY运算符与小于运算符和GROUP BY子句:
SELECT * FROM Teacher_Info WHERE Teacher_Salary < ANY (SELECT AVG ( Teacher_Salary ) from Teacher_Info GROUP BY Teacher_Dept_Id );
该查询显示了所有工资低于每个部门平均工资的教师的详细信息。
上述使用小于运算符的SELECT查询的输出如下表所示:
Teacher_Id | Teacher_First_Name | Teacher_Last_Name | Teacher_Dept_Id | Teacher_Address | Teacher_City | Teacher_Salary |
---|---|---|---|---|---|---|
1001 | Arush | Sharma | 4001 | 22 Street | New Delhi | 20000 |
1006 | Avinash | Sharma | 4002 | 12 Street | Delhi | 28000 |
1007 | Shyam | Besas | 4003 | 202 Street | Lucknow | 35000 |
以下查询使用ANY运算符和大于运算符以及GROUP BY子句:
SELECT * FROM Teacher_Info WHERE Teacher_Salary > ANY (SELECT AVG ( Teacher_Salary ) from Teacher_Info GROUP BY Teacher_Dept_Id );
这个查询展示了所有薪水高于每个部门平均薪水的教师的详细信息。
上述使用大于运算符的SELECT查询的输出如下表所示:
Teacher_Id | Teacher_First_Name | Teacher_Last_Name | Teacher_Dept_Id | Teacher_Address | Teacher_City | Teacher_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 |