Several common forms of MySQL subqueries

Source: Internet
Author: User

Common mysql subqueries:
Copy codeThe Code is as follows:
Select * from xxx where col = [any | all] (select * from xxxx );

This syntax can be divided into two types: keyword addition and keyword addition. When a keyword is not added, the subquery statement returns a discrete value (note that it is a discrete value ), the query statement uses the result of a subquery statement as the condition of its where clause. The syntax can include any, all, some, and other keywords before the subquery statement, the subquery statement returns a set of discrete values. Any indicates that the query statement uses the value returned by the subquery as a range to query the value range, which is similar to the in keyword. The key of all is not well understood, indicates all matched items.
Copy codeThe Code is as follows:
Select * from xxx where col in (select * from xxxx );

The syntax is quite clear, that is, the where clause of the query statement uses the result of the subquery statement as its range, which is similar to the any clause of the previous syntax;
Copy codeThe Code is as follows:
Select row (value1, value2......) = [any] (selectcol1, col2 ..);

The statement is executed in essence: The execution result of the subquery statement matches the query result set. If the matching result can be found, false is returned, the result sets on both sides are discrete values;
Copy codeThe Code is as follows:
Select... where col = [not] exists (select ......);

This statement is poor, and the execution is like this: when a subquery performs an operation and a result is returned, this statement will be executed and how many results are there, the statement is executed several times;
Copy codeThe Code is as follows:
Select... from (select...) as name where ......

This syntax is rarely used at ordinary times and is not easy to understand. In fact, this is the case. A new table is constructed through the execution results of the subquery (this table is a derived data table and a virtual table ), it is used as the object of the main sentence query. This syntax function is very powerful and is often used in some complex queries.

Although the subquery is quite convenient, it has many shortcomings. It does not support limit, and it has been proved by experiments that its execution efficiency is quite unsatisfactory. In general, or subqueries are not recommended.

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.