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 ~