UNIONinstruction is to merge the results of the two SQL statements. from this angle,UNIONis similar to Join, because both instructions can be captured from multiple forms.
One of the limits ofUNIONis that the column generated by the two SQL statements requires the same type of data. Also, when we use
When this instruction isUNION, we only see different data values (similar to Select Distance).
The grammar of
UNIONis as follows:
[SQL statement 1]
UNION
[SQL statement 2]
Suppose we have the following two forms,
Store_Informationtable
|
|||||||||||||||
Internet Salestable
|
and we have to find out all the days of sales (Sales). To achieve this, we use the following SQL statements:
SELECT Date FROM Store_Information
UNION
SELECT Date FROM Internet_Sales
result:
Date |
Jan-05-1999 |
Jan-07-1999 |
Jan-08-1999 |
Jan-10-1999 |
Jan-11-1999 |
Jan-12-1999 |
is worth noting that if we use it in any SQL statement (or two sentences together) ”
IfSELECT DISTINCT Date“, then we will get exactly the same result.
UNION ALLThe purpose of this instruction is to merge the results of the two SQL statements together.UNION ALLandUNIONThe difference isUNION ALLwill list each qualified data, regardless of the data value.
The grammar of
UNION ALLis as follows:
[SQL statement 1]
UNION ALL
[SQL statement 2]
We use the same example as the previous page to showUNION ALLand
The difference betweenUNION. It is also assumed that we have the following two forms,
Store_InformationForm
|
|||||||||||||||
Internet SalesForm
|
and we have to find the days when the turnover of stores and online turnover. To achieve this, we use the following SQL statements:
SELECT Date FROM Store_Information
UNION ALL
SELECT Date FROM Internet_Sales
result:
Date |
Jan-05-1999 |
Jan-07-1999 |
Jan-08-1999 |
Jan-08-1999 |
Jan-07-1999 |
Jan-10-1999 |
Jan-11-1999 |
Jan-12-1999 |
andUNIONinstructions are similar,INTERSECTalso processed the results produced by the two SQL statements. The difference is,UNIONBasically oneOR(If this value exists in the first sentence or the second sentence, it will be selected), andINTERSECTis more likeAND(This value must be selected in the first and second sentences).UNIONis a collection, andINTERSECTis intersection.
The grammar of
INTERSECTis as follows:
[SQL statement 1]
INTERSECT
[SQL statement 2]
Suppose we have the following two forms,
Store_InformationForm
|
|||||||||||||||
Internet SalesForm
|
and which day we have to find store transactions and online transactions. To achieve this, we use the following SQL statements:
SELECT Date FROM Store_Information
INTERSECT
SELECT Date FROM Internet_Sales
result:
Date |
Jan-07-1999 |
Please pay attention, in
Under theINTERSECTinstruction, different values will only be listed once.
MINUSinstructions are used on two SQL statements. It first finds the results produced by the first SQL statement, and then to see if these results are in the result of the second SQL statement. If so, then this information is removed and will not appear in the final result. If the result of the second SQL statement does not exist in the result of the first SQL statement, the information is abandoned.
The grammar of
MINUSis as follows:
[SQL statement 1]
MINUS
[SQL statement 2]
We continue to use the same example:
Store_InformationForm
|
|||||||||||||||
Internet SalesForm
|
and we need to know which day there is a store turnover without network turnover. To achieve this, we use the following SQL statements:
SELECT Date FROM Store_Information
MINUS
SELECT Date FROM Internet_Sales
Result:
Date |
Jan-05-1999 |
Jan-08-1999 |
“Jan-05-1999”, “Jan-07-1999”, and “Jan-08-1999″ is ”
The result ofSELECT Date FROM Store_Information“. In this,” JAN-1-1999 “exists in”
The result ofSELECT Date FROM Internet_Sales“. Therefore,” Jan-07-1999 “is not in the final result.
Please pay attention, in
Under theMINUSinstruction, different values will only be listed once.
Original address:http://sql.1keydata.com/cn/sql-union.php
http://sql.1keydata.com/cn/sql-unionall.php