Joint query of tables with the same mysql fields (use of union)

Source: Internet
Author: User

For tables with the same fields in mysql (used by union), cross-table statistics are required when multiple tables are maintained and used, after query, you can use the union and union all statements to implement union and union all. They combine the content of multiple tables and then output them as one table. Their difference is that union removes duplicate rows, while union all retains all of them. Note that: the columns and columns in all queries must be in the same order (the list content can include column names, arithmetic expressions, Aggregate functions, etc) the data type must be compatible (the corresponding columns in the result set must have the same data type, or implicit data conversion exists between the two data types, or type conversion is provided). The following is an example: there are two tables, TA and TB. Each table has the Name column and Size column. The target is to display the Size and of each Name. The statement is as follows:

[SQL] select name, sum (size) from (select name, size from TA union select name, size from TB) alias group by name;

 

Alias is the alias of the union table in parentheses.

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.