Home  >  Article  >  Database  >  How to use minus operator in mysql?

How to use minus operator in mysql?

青灯夜游
青灯夜游Original
2020-10-19 15:00:434875browse

Mysql does not support the MINUS operator, but you can use MySQL JOIN to simulate it. The syntax format is "SELECT field list FROM table 1 LEFT JOIN table 2 ON join_predicate WHERE table 2.id IS NULL;".

How to use minus operator in mysql?

(Recommended tutorial: mysql video tutorial)

Introduction to SQL MINUS operator

MINUS contains one of the three SQL standard operations UNION, INTERSECT and MINUS.

MINUS compares the results of two queries and returns different rows in the first query that are not output by the second query.

MINUS directive is used on two SQL statements. It first finds the results produced by the first SQL statement, and then checks to see if those results are among the results of the second SQL statement. If there is, then this data will be removed and will not appear in the final result. If the result produced by the second SQL statement does not exist in the result produced by the first SQL statement, the data is discarded.

The syntax of the MINUS operator is explained below:

SELECT column_list_1 FROM table_1
MINUS 
SELECT columns_list_2 FROM table_2;

The basic rules for queries using the MINUS operator are as follows:

  • Quantity and two columns The order column_list_1 and column_list_2 must be the same.

  • The data types of the corresponding columns in the two queries must be compatible.

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

CREATE TABLE t1 (
    id INT PRIMARY KEY
);
 
CREATE TABLE t2 (
    id INT PRIMARY KEY
);
 
INSERT INTO t1 VALUES (1),(2),(3);
INSERT INTO t2 VALUES (2),(3),(4);

The following query returns the different values ​​in the query for the t1 table, these values ​​are in the table t2 cannot be found in the query results.

SELECT id FROM t1
MINUS
SELECT id FROM t2;

How to use minus operator in mysql?

The following is the MINUS diagram description:

How to use minus operator in mysql?

MySQL MINUS operator

Unfortunately, MySQL does not support the MINUS operator. However, you can simulate it using MySQL JOIN.

To simulate MINUS two queries, use the following syntax:

SELECT 
    column_list 
FROM 
    table_1
    LEFT JOIN table_2 ON join_predicate
WHERE 
    table_2.id IS NULL;

For example, the following query uses the LEFT JOIN clause to return the same results as the MINUS operator:

SELECT 
    id
FROM
    t1
        LEFT JOIN
    t2 USING (id)
WHERE
    t2.id IS NULL;

The above is the detailed content of How to use minus operator in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn