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 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 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的一些示例。

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

    Output:

    输出:

    STATE COUNTRY
    Texas USA
    London UK
    国家
    德州 美国
    伦敦 英国

    SQL Except

    SQL除外

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

    Output:

    输出:

    STATE COUNTRY
    London UK
    Texas USA
    国家
    伦敦 英国
    德州 美国

    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:

    输出:

    STATE COUNTRY
    London UK
    California USA
    Texas USA
    国家
    伦敦 英国
    加利福尼亚州 美国
    德州 美国

    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除外语句

sql除外语句_SQL除外相关推荐

  1. sql视图语句_SQL视图:Replace View语句的示例语法

    sql视图语句 A View is a database object that presents data from in one or more tables. The same SQL stat ...

  2. sql delete语句_SQL Delete语句概述

    sql delete语句 This article on the SQL Delete is a part of the SQL essential series on key statements, ...

  3. sql update 语句_SQL Update语句概述

    sql update 语句 In this article, we'll walk-through the SQL update statement to modify one or more exi ...

  4. java传值到sql decode语句_SQL之DECODE

    select * from AORDER BY DECODE(Aa, NULL,Ab); Aa          Ab --------    ---------- 1            19 n ...

  5. laravel打印sql语句_SQL语句为什么慢?索引为什么失效?

    为什么你写的sql查询慢?为什么你建的索引常失效?通过本篇内容,你将学会MySQL性能下降的原因,索引的简介,索引创建的原则,explain命令的使用,以及explain输出字段的意义.助你了解索引, ...

  6. sql注入语句示例大全_SQL Order By语句:示例语法

    sql注入语句示例大全 Order By is a SQL command that lets you sort the resulting output from a SQL query. Orde ...

  7. sql注入语句示例大全_SQL Group By语句用示例语法解释

    sql注入语句示例大全 GROUP BY gives us a way to combine rows and aggregate data. GROUP BY为我们提供了一种合并行和汇总数据的方法. ...

  8. sql join语句语法_SQL Left Join语句:示例语法

    sql join语句语法 对于本指南,我们将讨论SQL LEFT JOIN. (For this guide we'll discuss the SQL LEFT JOIN.) Using the k ...

  9. 执行sql语句_SQL查询语句的执行顺序解析

    SQL语句执行顺序 结合上图,整理出如下伪SQL查询语句. 从这个顺序中我们可以发现,所有的查询语句都是从 FROM 开始执行的.在实际执行过程中,每个步骤都会为下一个步骤生成一个虚拟表,这个虚拟表将 ...

最新文章

  1. HTML5跳转页面并传值以及localStorage的用法
  2. ASP.NET-FineUI开发实践-14
  3. windows防火墙支持FTP服务的设置方法
  4. CCF 201703-3 Markdown
  5. 博弈论 斯坦福game theory stanford week 1.1
  6. 使用gparted live分区工具对VMware及ESXI(vsphere)虚拟机进行根目录扩容(可视化界面操作)
  7. c++错误之map的find()返回值类型
  8. 中文分词入门之字标注法4
  9. svn强制注释 linux,svn强制要求提交注释
  10. 深度学习pytorch--MNIST数据集
  11. fibonacci数列前20项_高考数学二级结论——数列部分
  12. Haproxy+Percona-XtraDB-Cluster 集群
  13. #if、#if defined 的使用
  14. python读取数据库中指定内容_python如何用正则表达式读取对应字段写入数据库中?...
  15. 用Photoshop制作简单贺卡
  16. Mysql 如何做双机热备和负载均衡 (方法一)
  17. 事务机制主要是利用undo、redo日志?
  18. 你要的《高性能MySQL》.pdf
  19. 程序读取凡人修仙传热度数据
  20. 小包实用工具:国家代码大全

热门文章

  1. install intel c/c++ compiler
  2. php和java的一些比较
  3. 《Python核心编程》第二版第209页第八章练习 -Python核心编程答案-自己做的-
  4. [转载] python查看的统计量_python 描述性统计_Python中的基本统计:描述性统计
  5. verilog之状态机详细解释(一)
  6. Linux(centos)下安装JDK
  7. Tomcat 8默认工具manager管理页面访问配置
  8. 【pytorch】pytorch-LSTM
  9. 微软Power BI 每月功能更新系列——3月Power BI 新功能学习
  10. mysql的学习笔记(六)