MySQL multi-table join query with union and UNION all usage

Source: Internet
Author: User

1. Preparation of two tables

Table A

Table B

2. Test the connection query:

(1) Cross connection (Cartesian product)

SELECT * from a a B

  

(2) Inner connection

Show internal connections

SELECT a.*,b.* from a INNER JOIN B on A.age=b.ageb

display the INNER join filter Condition:

SELECT a.*,b.* from a INNER JOIN B on A.age=b.ageb have a.age=11

Hermit inside Connection:

SELECT * from a b WHERE a.age=b.ageb

Hermit Connection Filtering results:

SELECT * from a b WHERE A.age=b.ageb having a.age=11

(3) Left outer connection:

  

(4) Right outer connection:

  

3.Union and Union All

(1) Union: Remove duplicate elements

SELECT * from Aunionselect * from B

  

Summarize:

Union: The union means to merge two or more query results together.
Requirements: Two queries must have the same number of columns
Recommendation: The type of column can be different, but the recommended query for each column, want the corresponding type to be the same
Data that can come from multiple tables: The column names taken out by multiple SQL statements can be inconsistent, with the column name of the first SQL statement as a standard.
If the rows taken in different statements are identical (in this case, the values for each column are the same), then union merges the same rows and eventually retains only one row. It is also possible to understand that union removes duplicate rows.
If you do not want to remove duplicate rows, you can use union ALL.
If there is an order by,limit in the clause, enclose it in parentheses (). After all clauses are recommended, the result of the final merge is sorted or filtered.

(2) UNION All retains duplicate elements

The union ALL command is almost equivalent to the Union command, but the union ALL command lists all values.

SELECT * from Aunion allselect * from B

  

Summarize:

The UNION is used to combine the result set of two or more SELECT statements and eliminate any duplicate rows in the table.
The SELECT statement inside the UNION must have the same number of columns, and the column must have a similar data type.
Also, the order of the columns in each SELECT statement must be the same.

By default, the UNION operator chooses a different value. If duplicate values are allowed, use UNION all.
When all is used with the Union (that is, union ALL), duplicate rows are not eliminated

Attention:

1. The column name in the UNION result set is always equal to the column name in the first SELECT statement
2. The SELECT statement inside the UNION must have the same number of columns. The column must also have a similar data type. Also, the order of the columns in each SELECT statement must be the same

MySQL multi-table join query with union and UNION all usage

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.