One: minus directive
The
is used on two SQL statements.
It first finds out the results of the first SQL statement, and then looks at whether the results are in the result of a second SQL statement.
If so, the first SQL result data is removed, not in the final result.
If the result of the first SQL statement does not exist in the result of the second SQL statement,
the first SQL result data is retained and will appear in the final result. The syntax for
minus is as follows:
[SQL statement 1]
minus
[SQL statement 2]
Example:
store_information table
store_name sales Date
Los angeles $1500 jan-05-1999
San diego $250 jan-07-1999
Los angeles $300& nbsp jan-08-1999
boston $700 jan-08-1999
Internet sales Form date Sales
jan-07-1999& nbsp $
jan-10-1999 $535
jan-11-1999 $320
jan-12-1999 $750
We use the following SQL statement:
Select date from Store_information
minus
Select date from Internet_sales
Result:
Date
jan-05-1999
jan-08-1999
Explain:
"jan-05-1999", "jan-07-1999", and "jan-08-1999" are the result of "select Date from Store_information".
In this case, "jan-07-1999" is present in the result of "select Date from Internet_sales".
So "jan-07-1999" is not in the final result.
Please note that under the minus directive, different values are only listed once.
Similar to the minus function, there are
First one
SELECT Table 1.*
From table 1, table 2
WHERE table 1. Primary KEY = Table 2. Primary KEY (+)
and table 2. The primary key is NULL;
A second
SELECT * FROM table 1 where NOT EXISTS (select 1 from table 2 where table 1. primary KEY = Table 2. primary key);
A third
SELECT * FROM table 1 where table 1. Primary key not in (select Primary key from table 2);
Of course, high efficiency is still minus.
II: UNION directive
It merges the results of two SQL statements.
From this point of view, UNION is somewhat similar to join, because these two instructions can be retrieved from multiple tables.
One limitation of the UNION is that the columns generated by the two SQL statements need to be of the same type of data.
Also, when we use the Union command, we will only see different data values (like SELECT DISTINCT).
The syntax of UNION is as follows:
[SQL Statement 1]
UNION
[SQL Statement 2]
Example:
Table: Store_information
Store_name Sales Date
Los Angeles $1500 jan-05-1999
San Diego $ jan-07-1999
Los Angeles jan-08-1999
Boston $700 jan-08-1999
Table: Internet Sales
Date Sales
jan-07-1999
jan-10-1999 $535
jan-11-1999 $320
jan-12-1999 $750
We use the following SQL statement:
SELECT Date from Store_information
UNION
SELECT Date from Internet_sales
Results:
Date
jan-05-1999
jan-07-1999
jan-08-1999
jan-10-1999
jan-11-1999
jan-12-1999
Description
If we use "Select DISTINCT Date" in any SQL statement (or both),
Then we'll get exactly the same result.
Names can be different. However, the data types must be similar.
That is, the first column of the primary table (the first table) is varchar (100), and the first column of the secondary table (all tables except the first one) is varchar (100&&<100)
(char appears to be possible, or can automatically convert data types), others are not, and several tables must have the same number of columns.
Note: The essay content is from the online data collation, for reference only.
The use of minus in SQL and the use of Union