Simple tutorial on SQL statements in linux --- UNION, simple tutorial --- union

Source: Internet
Author: User

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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.