How to Implement Multi-table non-join queries in mysql

Source: Internet
Author: User

How to Implement Multi-table non-join queries in mysql

Everyone is usingMySQLWhen querying, it is normal to directly query a table, or even join queries for multiple tables. left join and right join are used), inner join, and outer join ). There is a certain relationship between the two tables, that is, we often say there is a foreign key ing, you can use the. id = B. aId statement to write the relationship. This is often used by everyone, but sometimes we need to query two or more tables at the same time, these tables are not correlated, for example, to query some data in the user table and user_history table, this is a non-join query.

In this case, the union all statement is used. For example:

</Pre> <pre class = "html" name = "code"> (select name, sex, age from user where name like 'King % ') union all (select name, sex, age from user_history where name like 'wang % ');

This statement is used to query the information of the User table and all the persons with the surname Wang in the History Table. This operation can also be sorted and intercepted,

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

This is the person who obtained the first 50 of the two tables by age.

The above is a small Editor for you to talk about the implementation of multi-table non-join queries in mysql All content, I hope you can support a lot of help house ~

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.