A2-02-22.DML-MySQL MINUS

轉載自:http://www.mysqltutorial.org/mysql-minus/mysql

MySQL MINUS(本身本次沒有測試出預期的結果,估計與MySQL版本和SQL模式有關)

 

Summary: in this tutorial, you will learn how about SQL MINUS operator and how to simulate MySQL MINUS operator using join.sql

Introduction to SQL MINUS operator

MINUS is one of three set operations in the SQL standard that includes UNIONINTERSECT, and MINUS.app

MINUS compares results of two queries and returns distinct rows from the first query that aren’t output by the second query.post

The following illustrates the syntax of the MINUS operator:測試

The basic rules for a query that uses MINUS operator are the following:this

  • The number and order of columns in both column_list_1 and column_list_2 must be the same.
  • The data types of the corresponding columns in both queries must be compatible.

Suppose we have two tables t1 and t2 with the following structure and data:spa

The following query returns distinct values from the query of the t1 table that are not found on the result of the query of the t2 table.code

MySQL MINUS Example

The following Venn diagram illustrates the MINUS operator:blog

MySQL MINUS Operator Illustration

Note that some database systems e.g., Microsoft SQL Server, PostgreSQL, etc., use the EXCEPT instead of MINUS, which have the same function.three

MySQL MINUS operator

Unfortunately, MySQL does not support MINUS operator. However, you can use the MySQL join to simulate it.

To emulate the MINUS of two queries, you use the following syntax:

For example, the following query uses the LEFT JOIN clause to return the same result as the MINUSoperator:

In this tutorial, you have learned about the SQL MINUS operator and how to implement MySQL MINUS operator using LEFT JOIN clause.

相關文章
相關標籤/搜索