The Union of MySQL

Source: Internet
Author: User

Write about the use of Union today and something to be aware of.

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.

such as: (SELECT * from a ORDER by ID) union (SELECT * from B order ID);

In clauses, the order by needs to be used with limit to make sense. If not used with limit, it will be removed by the parser optimization analysis.

The Union of MySQL

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.