MySQL learning -- join and union usage _ MySQL

Source: Internet
Author: User
MySQL learning-the use of join and union I feel I have been using the framework since work, and some basic things of the database have been forgotten. I will review the old knowledge through this series of blogs, learn new knowledge.

Today we will start with join and union.

Join is a record set generated by the records with the same conditions in the two tables after the join operation,
Union is the combination of two records to form a new record set.

Join

The following figure shows the join command.

Union

Required: the number of columns in the two queries must be consistent.

Recommendation: the types of columns can be different, but we recommend that you query each column, and you want the corresponding types to be the same

Data from multiple tables: the names of the columns retrieved from multiple SQL statements may be inconsistent. the column names of the first SQL statement shall prevail.

If the rows extracted from different statements are identical (the values of each column are the same), union merges the same rows and retains only one row. We can also understand that union removes duplicate rows.

If you do not want to remove duplicate rows, you can use union all.

If the clause contains order by and limit, enclose them with parentheses. It is recommended to put all the clauses, that is, sort or filter the final merged results.

For example: (select * from a order by id) union (select * from B order id );

In a clause, order by must be used with limit to make sense. If it is not used with limit, it will be removed when the syntax analyzer optimizes the analysis.

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.