sql除外语句
We usually join two tables in order to get a combined result set. But, there are cases when we want a result set which is available only in one table and not available in the other table. SQL provides a feature called Except. Exception literally means not included. SQL except is also very similar to the same concept.
我们通常将两个表连接起来以获得组合的结果集。 但是,在某些情况下,我们希望结果集仅在一个表中可用,而在另一表中不可用。 SQL提供了一个称为Except的功能。 从字面上看,异常意味着不包括在内。 SQL除了与同一个概念非常相似。
SQL除外 (SQL Except)
The Except clause is used to return all rows in the first SELECT statement that is not returned by the second SELECT statement. Both the SELECT statements will return two different datasets. The EXCEPT operator will retrieve all the result set from the first SELECT query and will remove the duplicates from the second SELECT query.
Except子句用于返回第二个SELECT语句未返回的第一条SELECT语句中的所有行。 这两个SELECT语句都将返回两个不同的数据集。 EXCEPT运算符将从第一个SELECT查询中检索所有结果集,并将从第二个SELECT查询中删除重复项。
SQL除外的使用规则 (Rules for Usage of SQL Except)
- The columns that you wish to compare between two SELECT statements need not have to be same fields but the corresponding columns should have the same data type. 您希望在两个SELECT语句之间进行比较的列不必具有相同的字段,但是对应的列应具有相同的数据类型。
- There must be the same number of expressions in both SELECT statements. 两个SELECT语句中必须有相同数量的表达式。
- The corresponding columns in each of the SELECT statements must have similar data types. 每个SELECT语句中的对应列必须具有相似的数据类型。
- The EXCEPT operator returns all records from the first SELECT statement that are not in the second SELECT statement. EXCEPT运算符从第一个SELECT语句返回不在第二个SELECT语句中的所有记录。
- The EXCEPT operator in SQL Server is equivalent to the MINUS operator in Oracle. SQL Server中的EXCEPT运算符等效于Oracle中的MINUS运算符。
SQL除外语法 (SQL Except Syntax)
SELECT column_name[s] from table1
EXCEPT
SELECT column_name[s] from table2;
SQL除外示例 (SQL Except Example)
Let us consider the following two tables for SQL Except
让我们考虑以下两个表,除了SQL
Customer Table
客户表
CUSTOMER ID | CUSTOMER NAME | STATE | COUNTRY |
---|---|---|---|
1 | Akash | Delhi | India |
2 | Amit | Hyderabad | India |
3 | Jason | California | USA |
4 | John | Texas | USA |
5 | Simon | London | UK |
顾客ID | 顾客姓名 | 州 | 国家 |
---|---|---|---|
1个 | 阿卡什 | 新德里 | 印度 |
2 | 阿米特 | 海得拉巴 | 印度 |
3 | 杰森 | 加利福尼亚州 | 美国 |
4 | 约翰 | 德州 | 美国 |
5 | 西蒙 | 伦敦 | 英国 |
Supplier Table
供应商表
SUPPLIER ID | SUPPLIER NAME | STATE | COUNTRY |
---|---|---|---|
1 | Apple | California | USA |
2 | TCS | Hyderabad | India |
3 | Information System | Delhi | India |
4 | Solar Energy | Bangalore | India |
供应商编号 | 供应商名称 | 州 | 国家 |
---|---|---|---|
1个 | 苹果 | 加利福尼亚州 | 美国 |
2 | TCS | 海得拉巴 | 印度 |
3 | 信息系统 | 新德里 | 印度 |
4 | 太阳能 | 班加罗尔 | 印度 |
Here is the script for the creation of tables and insertion of sample data in the PostgreSQL database.
这是用于创建表和在PostgreSQL数据库中插入示例数据的脚本。
CREATE TABLE public."Customer"
(
"Customer_Id" bigint NOT NULL,
"Customer_Name" character varying(50) COLLATE pg_catalog."default" NOT NULL,
"State" character varying(20) COLLATE pg_catalog."default" NOT NULL,
"Country" character varying(20) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT "Customer_pkey" PRIMARY KEY ("Customer_Id")
)
CREATE TABLE public."Supplier"
(
"Supplier_Id" bigint NOT NULL,
"Supplier_Name" character varying(50) COLLATE pg_catalog."default" NOT NULL,
"State" character varying(20) COLLATE pg_catalog."default" NOT NULL,
"Country" character varying(20) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT "Supplier_pkey" PRIMARY KEY ("Supplier_Id")
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
INSERT INTO public."Customer"("Customer_Id", "Customer_Name", "State", "Country")VALUES (1, 'Akash', 'Delhi', 'India'),
(2, 'Amit', 'Hyderabad', 'India'),
(3, 'Jason', 'California', 'USA'),
(4, 'John', 'Texas', 'USA'),
(5,'Simon','London','UK');
INSERT INTO public."Supplier"("Supplier_Id", "Supplier_Name", "State", "Country")VALUES (1, 'Apple', 'California', 'USA'),
(2, 'TCS', 'Hyderabad', 'India'),
(3, 'Information System', 'Delhi', 'India'),
(4, 'Solar Energy', 'Bangalore', 'India');
Let’s look into some example for SQL Except using these tables.
我们来看一下使用这些表SQL Except的一些示例。
- SQL Except SQL除外
Select "State" ,"Country" from "Customer" Except Select "State","Country" from "Supplier";
Output:
输出:
STATE COUNTRY Texas USA London UK 州 国家 德州 美国 伦敦 英国 - SQL Except with Order By SQL顺序除外
Select "State" ,"Country" from "Customer" Except Select "State","Country" from "Supplier" order by "State"
Output:
输出:
STATE COUNTRY London UK Texas USA 州 国家 伦敦 英国 德州 美国 In the above query, the result set is sorted based on the State Column
在上面的查询中,结果集基于“状态列”进行排序
- SQL Except using Country Column 除使用“国家/地区”列外SQL
Select "State" ,"Country" from "Customer" Except Select "State","Country" from "Supplier" where "Country" = 'India'
Output:
输出:
STATE COUNTRY London UK California USA Texas USA 州 国家 伦敦 英国 加利福尼亚州 美国 德州 美国 In the above query, the first SELECT query gets all the rows and from the second SELECT statement it gets the rows where the country is India and corresponding rows are removed from the first SELECT query.
在上面的查询中,第一个SELECT查询获取所有行,并从第二个SELECT语句获取国家/地区为印度的行,并从第一个SELECT查询中删除相应的行。
sql除外语句