Simple tutorial on SQL statements in linux --- UNION, simple tutorial --- union
UNIONThe purpose of the command is to combine the results of two SQL statements. From this perspective,UNIONThis is somewhat similar to JOIN, because both commands can retrieve data from multiple tables.UNIONOne restriction is that the columns generated by two SQL statements must be of the same data type. In addition, when we useUNIONIn this command, we only see different data values (similarSELECT DISTINCT).
UNIONSyntax:
[SQL statement 1]
UNION
[SQL statement 2];
Suppose we have two tables,
Store_InformationTable
Store_Name |
Sales |
Txn_Date |
Los Angeles |
1500 |
05-Jan 1999 |
San Diego |
250 |
07-Jan-1999 |
Los Angeles |
300 |
08-Jan 1999 |
Boston |
700 |
08-Jan 1999 |
Internet SalesTable
Txn_Date |
Sales |
07-Jan-1999 |
250 |
10-Jan 1999 |
535 |
11-Jan 1999 |
320 |
12-Jan 1999 |
750 |
We need to find out all the days with a turnover. To achieve this goal, we use the following SQL statement:
SELECT Txn_Date FROM Store_Information
UNION
SELECT Txn_Date FROM Internet_Sales;
Result:
Txn_Date |
Jan-05-1999 |
Jan-07-1999 |
Jan-08-1999 |
Jan-10-1999 |
Jan-11-1999 |
Jan-12-1999 |
It is worth noting that if we useSelect distinct Txn_DateThen we will get the same result.
Linux test results:
1. Create a table
2. Insert table records
3. UNION (and) two tables