A tutorial on using the From and exists clauses in a Mysql subquery _mysql

Source: Internet
Author: User

From child query
subqueries in the FROM clause
The MySQL from subquery refers to the FROM clause as a subquery statement, and the main query to the subquery results to obtain the required data. The FROM subquery syntax is as follows:

SELECT ... From (subquery) as name ...

A subquery generates a temporary table, and as name is required because each table in the FROM clause must have a name. The from subquery is also called a derived data table subquery.
From child query instance
Table1:

S1 s2
1  5
2
3  20

The From subquery SQL is as follows:
Select S1,s2 from (select S1, s2*2 as S2 from table1) as temp WHERE S1 > 1
The query returns the results shown below:

S1 s2
2
3 40

Tips
A subquery in the MySQL FROM clause can return a scalar, column, row, or table, but not a subquery that has an associated query.

MySQL subquery EXISTS and not EXISTS
MySQL EXISTS and not EXISTS subqueries
The MySQL EXISTS and not EXISTS subquery syntax is as follows:

SELECT ... From table WHERE EXISTS (subquery)

The syntax can be understood to be that the data of the main query is placed in a subquery for conditional validation, depending on the validation result (TRUE or FALSE) to determine whether the data results of the main query are preserved.
MySQL EXISTS Sub Query instance
The following is a practical example to understand the EXISTS subquery. The following are the original data tables:
Article Article table:

User table:

We want to identify the data in the article table, but require the UID to exist in the user table. The SQL statement is as follows:

SELECT * from article where EXISTS (SELECT * from user where article.uid = user.uid)

Return query results as follows:

As you can see from the execution of the statement, the 4th record in the article table is not preserved because the record's data returns FALSE in the subquery.
When the above SQL uses not EXISTS, the result of the query is the data record in the article table where the UID does not exist in the user table.
Tips
EXISTS (subquery) returns TRUE or FALSE only, so the SELECT * in a subquery can also be select 1 or otherwise, and the official claim is that the select list is ignored when actually executed, so there is no difference.
The actual execution of the EXISTS subquery may be optimized rather than a one-article comparison of our understanding, and if the problem of efficiency is concerned, the actual test can be conducted to determine whether the problem is efficient.
EXISTS subqueries can often be replaced with conditional expressions, other subqueries, or joins, and what is the optimal need for concrete analysis of specific problems.

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.