MySQL Chapter 3 - nested query

Nested query  

      SQL statement, a select - from - where block statement is a query, a query block in a nested condition where the words or blocks having another query phrase in the query is referred to as nested queries.

      Outer query or the parent query , the inner query or subquery

      SQL language allows nested queries, ie, a sub-query can also nest other sub-queries. Special attention: the SELECT statement subquery can not use the order by clause, order by clause can only sort the final results .

      Nested query so that users can use a plurality of simple queries a complex query, thereby enhancing the SQL query capabilities. In a way to constitute a deeply nested SQL in the program is the " structured " meaning lies .

  • IN predicate with a subquery

            In the nested query, the query result is often a sub-set of mistranslation predicate IN predicate nested query is most frequently used.

example:

In this example, the query does not depend on subquery parent query is not correlated subquery , performed from the inside out, to execute sub-queries, and then the results of the subquery as the parent query search condition , another method with its own connection to complete the query, as follows:

  • Subqueries with comparison operators

            Subquery comparison operator means with the connection between the parent and child queries Query comparison operators . When the user can know exactly the inner query returns a single value , it is possible using>, <,> =, <=,! =, Or <> comparison operator and the like ( when the inner query result is a value IN predicate can be = instead of )

Query sub-query is dependent on the parent query, such sub-query is a correlated subquery , the entire program statement referred to the relevant nested query .

example:

sc x is an alias table, known as a tuple variable can be used to represent a tuple of sc . Seeking a grade point average of all student electives inner query, as to which is the average score of students depends on the parameter values x.sno, and the value is associated with the parent query, so this type of query is a correlated subquery.

Solving the relevant sub-query can not be solved as irrelevant as a subquery subquery to solve it, then solving the parent query. Since the inner query and the outer query about, it must be repeatedly evaluated.

  • Subqueries with ANY (SOME) or ALL predicate

            Subqueries can use comparison operators return a single value, return multiple values ​​using comparison operators when something must use ANY (some systems SOME) or ALL predicate modifier, and use ANY or ALL predicate. as follows:

In fact, with subqueries aggregate functions are generally more efficient than directly to the ANY or ALL query corresponds ANY, ALL relationship with aggregate functions as follows:

  • Subqueries with exists predicate

              Representative existential quantifier predicate exists, exists with subquery predicate does not return any data, produced only a logical true value "true" or logically false value "false"

              

example:

Some sub-queries exists or not exists predicate can not be other forms of sub-query is equivalent to replace, but all with in predicates, comparison operators, any and all sub-query predicates can use sub-query predicate exists with equivalent replacement .

Because the sub-band of interest only exists quantifier inner query queries whether the return value does not need to check the specific means, so the efficiency is not necessarily correlated subquery not less than, and sometimes an efficient method.

 

 

Guess you like

Origin www.cnblogs.com/lxx2/p/12561759.html