sql除外语句_SQL除外

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 Except diagram

SQL Except Representation

SQL除外表示

SQL除外语法 (SQL Except Syntax)

SELECT column_name[s] from table1
EXCEPT
SELECT column_name[s] from table2;
MySQL does not support SQL Except clause. I am using PostgreSQL database in this tutorial to show SQL Except examples.
MySQL不支持SQL Except子句。 我在本教程中使用PostgreSQL数据库来显示SQL Except示例。

SQL除外示例 (SQL Except Example)

Let us consider the following two tables for SQL Except

让我们考虑以下两个表,除了SQL

Customer Table

客户表

CUSTOMER IDCUSTOMER NAMESTATECOUNTRY
1AkashDelhiIndia
2AmitHyderabadIndia
3JasonCaliforniaUSA
4JohnTexasUSA
5SimonLondonUK
顾客ID 顾客姓名 国家
1个 阿卡什 新德里 印度
2 阿米特 海得拉巴 印度
3 杰森 加利福尼亚州 美国
4 约翰 德州 美国
5 西蒙 伦敦 英国

Supplier Table

供应商表

SUPPLIER IDSUPPLIER NAMESTATECOUNTRY
1AppleCaliforniaUSA
2TCSHyderabadIndia
3Information SystemDelhiIndia
4Solar EnergyBangaloreIndia
供应商编号 供应商名称 国家
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的一些示例。

  1. SQL Except

    SQL除外
  2. Select "State" ,"Country" from "Customer"
    Except
    Select "State","Country" from "Supplier";

    Output:

    输出:

    STATECOUNTRY
    TexasUSA
    LondonUK
    国家
    德州 美国
    伦敦 英国
    sql except example

    SQL Except

    SQL除外

  3. SQL Except with Order By

    SQL顺序除外
  4. Select "State" ,"Country" from "Customer"
    Except
    Select "State","Country" from "Supplier" order by "State"

    Output:

    输出:

    STATECOUNTRY
    LondonUK
    TexasUSA
    国家
    伦敦 英国
    德州 美国
    sql except order by

    SQL Except Using Order By Clause

    除按子句使用顺序外SQL

    In the above query, the result set is sorted based on the State Column

    在上面的查询中,结果集基于“状态列”进行排序

  5. SQL Except using Country Column

    除使用“国家/地区”列外SQL
  6. Select "State" ,"Country" from "Customer"
    Except
    Select "State","Country" from "Supplier" where "Country" = 'India'

    Output:

    输出:

    STATECOUNTRY
    LondonUK
    CaliforniaUSA
    TexasUSA
    国家
    伦敦 英国
    加利福尼亚州 美国
    德州 美国
    sql except with where clause

    SQL Except using Where Clause

    SQL除了使用Where子句

    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查询中删除相应的行。

翻译自: https://www.journaldev.com/23888/sql-except

sql除外语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值