Home  >  Article  >  Database  >  Detailed explanation of the implementation method of multi-table non-correlated query in mysql

Detailed explanation of the implementation method of multi-table non-correlated query in mysql

怪我咯
怪我咯Original
2017-07-06 11:29:201441browse

The following editor will bring you a brief discussion on the implementation method of ##mysqlmulti-table non-correlatedquery. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

When you use

MySQL to query, it is usually a direct query of one table, or it is a related query of multiple tables. Left join (left join), right join (right join), inner join (inner join), and outer join (outer join) are used. This kind of relationship has a certain relationship between the two tables, which is what we often say has a foreign key corresponding relationship, which can be written using the statement a.id = b.aId. This is commonly used by everyone, but sometimes we need to query two or more tables at the same time, and these tables are not related to each other. For example, if we want to query certain data in the user table and user_history table, This time is the so-called non-correlated query.

The

union all statement is used at this time. For example:

 
(select name,sex,age from user where name like '王%' ) union all (select name,sex,age from user_history where name like '王%' ) ;

This statement is used to query the information of all persons with the surname Wang in the user table and history table. This can also be sorted and intercepted.

(select name,sex,age from user where name like '王%' ) union all (select name,sex,age from user_history where name like '王%' ) order by age desc limit 0,50;

This is to get the top 50 people sorted by age in these two tables.

The above is the detailed content of Detailed explanation of the implementation method of multi-table non-correlated query in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn