On the implementation of multi-table non-associative query in Mysql _mysql

Source: Internet
Author: User
Tags join

We use MySQL query when the normal is directly a table query, or that is, multiple tables of the associated query, use to the left-join (right) join, the inner join, the outer join (outer join). This is a certain correlation between two tables, that is, we often say that there is a foreign key corresponding relationship, you can use to a.ID = B.aid This statement to write the relationship. This is commonly used, but sometimes we will need to query two or more tables at the same time, these tables are not related to each other, such as to check the user table and User_history table some data, this time is called the unrelated query.

The UNION ALL statement is used at this time. Like what:

</pre> <pre class= "html" name= "code" > (select name,sex,age from user where name like ' King% ') union ALL (select N Ame,sex,age from User_history where name like ' King% ');

This statement is the information that is used to query the user's table and all the names of the people in the history table. The same can be sorted, interception operations,

(select Name,sex,age from user where name like ' King% ') union ALL (select Name,sex,age from user_history where name like ' Wang% ') Order BY age desc limit 0, 50;

This is the person who gets the top 50 of the two tables by age.

The above is a small series for everyone to talk about the implementation of multiple tables in MySQL not related query all the content, I hope that we support the cloud-Habitat Community ~

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.