Home Database Mysql Tutorial 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

Jul 06, 2017 am 11:29 AM
mysql accomplish method

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:

</pre> <pre class="html" name="code">(select name,sex,age from user where name like '王%' ) union all (select name,sex,age from user_history where name like '王%' ) ;
Copy after login

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;
Copy after login

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 of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs)

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

How to set font size on mobile phone (easily adjust font size on mobile phone)

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors) May 07, 2024 pm 05:55 PM

How to choose a mobile phone screen protector to protect your mobile phone screen (several key points and tips for purchasing mobile phone screen protectors)

What are the application scenarios of Java enumeration types in databases? What are the application scenarios of Java enumeration types in databases? May 05, 2024 am 09:06 AM

What are the application scenarios of Java enumeration types in databases?

See all articles