Subquery usage in MySQL

Subqueries

 The Importance
1.select words constitute
the SELECT ...
from ...
the WHERE ...
Group by
the HAVING ...
the Order by

 subquery

1. What is the sub-query?
Queries inside nested query, that is, there is also select select
select ... (select)
from ... (select)
the WHERE ... (select)
Group by
the HAVING ... (select)
the Order by
nested select clause may appear in more locations

2. The sub-query syntax
subquery (inner query) before execution, and executes the main query (outer query)
results returned by the subquery is used in the outer query
subqueries may occur in almost all the SELECT clause, such as: SELECT sub sentence, FROM clause, WHERE clause, oRDER BY clause, HAVING clause and other
sub-query must be placed in parentheses in
sub-query comparison operators are generally on the right side, in order to enhance code readability

3. sub-query classification
is not correlated subquery
subquery is not used in any list of the outer query. First implementation of sub-queries, and then do the outer query
correlated subquery (correlated subquery)
subquery to use any list of the outer query. Outer query executed first, and then perform the subquery
or more below can be divided into two types:
line subquery (row subquery): result set returned rows and N columns is a
column subqueries (column subquery): The result set is returned N a row
table subquery (table subquery): the result set is returned N rows and N columns
scalar query (scalar subquery): returns a value of a row 1

 scalar query
1. Query scalar (scalar subquery): Returns a value of a line 1
can => <> = <= <> result of the operator is compared subquery

2. What will be used when subqueries it?
(1) When we write select words, find the need to write a number of select words to get results, and the relationship between the dependent association of a number of select words, dependency usually write back and select where behind, then we the number of select words together to write.
For example: to select two words, for example: the results of a select clause dependent on a second select words, we will use a subquery

Example. 1: (. 1) SELECT name from the PLAYERS WHERE PLAYERNO = (SELECT PLAYERNO from of TEAMS WHERE TEAMNO =. 1);
(2) SELECT PLAYERNO from of TEAMS WHERE TEAMNO =. 1;
SELECT name from the PLAYERS WHERE PLAYERNO =. 6;
(. 1), and ( 2) the result is the same as
Here Insert Picture Descriptionexample 2:
the SELECT playerno, name, Joined - (the WHERE playerno the SELECT Joined the fROM Players = 104) year_interval
## scalar query from the alias year_interval
the fROM Players
the WHERE playerno <60;
statement Definition: for No less than 60 players to obtain a difference between the Current year they join the club and the club's players to join No. 104 Here Insert Picture Descriptionexample. 3:
the SELECT playerno, NAME
the FROM players
the WHERE the birth_date <(scalar query ##, less than, greater than, equal to, etc. comparison operators typically performed after where; +, - and other operators generally performed before where the words
the SELECT birth_date
the FROM the players
the wHERE leagueno = '6524');
sentences to define: query birthday less than coalition members numbered 9999 players birthday of the player's number and nameHere Insert Picture Description
 subquery row (single row multi-column)
row subquery (row subquery): returns the result set is 1 rows and N columns
using the compare line expression, can be used => <> = <= < > in character operation
example:
the SELECT name, sex, town, playerno ## column query name, sex row, column and town player columns
FROM players
the WHERE (Sex, town) = (the sELECT Sex, ## row subqueries town, sex, and select a value equal town
FROM players
= 104 playerno the WHERE)
and playerno! = ## will be equal to the filter 104 out of 104

Statement example: Query 104 and the player of the same sex and live in the same city player ID Here Insert Picture Description column subquery (single row multi-column)
1. column subquery (column subquery): returns the result set is a row N
2 must IN , ANY and ALL operator returns the sub-query results are compared. Wherein, the ANY and ALL operator can not be used alone, it must be preceded by a single row comparison operator => <> = <= <>
. 3. <= All (result set) or less all of the interpretation, i.e. seeking minimum there are also <all,> all,> = all other

any (result set) which is greater than any of the interpretation to a, is greater than the minimum, in addition to <any, <= any,> = any other

4. When used in column subquery? ?
(1) a plurality of requirements need SQL (select)
(2) need to select between several interdependent

Example. 1:
the SELECT playerno, NAME
the FROM the PLAYERS
the WHERE playerno the IN (## playerno in (the SELECT clause of the query to a single row multi-column data)
the SELECT playerno
the FROM The matches);

Interpretation Statement: Query players participated in at least one game of numbers and names
Note: players participate in the competition table (matches table), only the player numbers, player names in the players list, so the query will use multiple select statements, You can use the query Liezi

Example 2:
the SELECT playerno, NAME, the birth_date
the FROM the PLAYERS
the WHERE the birth_date <= ALL (## <= All interpretation or less all, behind a multi-line result set one of
the SELECT the birth_date
the FROM the PLAYERS);
statement Interpretation: Query the most old player number, name and date of birth. The oldest player refers to the birth date is less than equal to all other players players

Example 3:
the SELECT playerno, NAME, birth_date
the FROM the Players
the WHERE birth_date> the ANY (## result sets larger than any of
the SELECT birth_date
the FROM the Players);
statement Interpretation: In addition to query the oldest player, all other players number, name and birthday

5. Note: If the result of the sub-set of the query has a null value, use> ALL and not in when the operator must remove sub-query result set null value, otherwise an error results
we can focus on the use of statements where the result will be null value filter off
example:
the SELECT leagueno, playerno
the FROM Players
the wHERE leagueno> = ALL (
the SELECT leagueno
the FROM Players
the wHERE leagueno the iS the NOT nULL); ## focus on the use statement where the null values result filtered
Here Insert Picture DescriptionFurther, the use of null is not in the result set value filter

 subquery table (rows and columns)
1. subquery table (table subquery): result set is returned N rows and N columns
must IN result, ANY and ALL operator returns subquery comparing
Example:
the SELECT *
the FROM committee_members
the wHERE (BEGIN_DATE, END_DATE) the iN (## in the result set back may be more than one row, after where may be a plurality of columns, a table subquery (rows and columns)
the SELECT BEGIN_DATE, END_DATE
the FROM committee_members
the wHERE position = ' Secretary ');
statements Interpretation: in committee_members table, get working date and the date the same row with the outgoing Secretary positions of all rows

The inline view
used in the from clause subquery is called inline view (inline view). It serves as a data source, then the outer query to retrieve rows from which
the view must be defined with an alias, or error
exemplary:
the SELECT playerno
the FROM (the SELECT playerno, Sex ## () which we call the inline view
the FROM Players
the WHERE playerno <10) acting AS player10 ## as the aliases, and where select Also, note
column name must appear in the subquery (which example is PLAYERNO and Sex)
the wHERE Sex = 'M';
statement interpretation: to give No less than 10 players in the men's number

子查询中再嵌套子查询
示例:
SELECT playerno
FROM
(SELECT playerno, sex
FROM
(SELECT playerno, sex, joined
FROM
(SELECT playerno, sex, joined
FROM players
WHERE playerno > 10) great10
WHERE playerno < 100) less100
WHERE joined > 1980) joined1980
WHERE sex = ‘M’ ;

Note: For the select clause, the order of execution to execute sub-queries, and subqueries nested subquery, perform innermost subquery, such as the above first implementation example green, yellow performed, in performing blue, Finally, the implementation of select words no color section.

 correlated subquery
1. correlated subquery (correlated subquery)
subquery to use any list of the outer query. First implementation of the outer query, and then execute the sub-queries

2. Related step subquery
1), perform the outer query, the row is called the candidate lines obtained
2), is performed using a subquery candidate row
3), using the return value of the sub-query to determine the candidate line appeared in the final result set is discarded or
4) repeat steps 2 and 3 above, the processing is completed for all of the candidate lines to obtain a final result

Note: where conditions have subquery columns of the main query
example:
the SELECT matchno, playerno, teamno
the FROM ## m The matches alias m
the WHERE playerno = (
the SELECT playerno
the FROM ## alias Teams T T
the WHERE playerno = m.playerno);
Statement Interpretation: for the captain of the team to participate in every game, to get the race number, number and team captain No.
statement analysis: is the result of holding the outer query to compare the results of a query, if the two results match, the result of the reservation, otherwise, the result will abandon

When using a correlated subquery?
(1) requires the use of sub-queries
(2) two independent sub-queries can not be executed

EXISTS operator
1. The operator specialized sub-query result set to determine whether or not empty: if you do not return true if empty, false otherwise
nothing exists operator is only considered if data is returned, regardless of the data is returned .
2. When using EXISTS operator
commonly used in the relevant sub-query (1)
(2) at least needs often keywords

Example:
the SELECT NAME, ## Initials Initials initials represent
the FROM the Players the p-
the WHERE EXISTS (## EXISTS represented behind the select statement if data is returned
SELECT 1 ## If a row of data is returned 1, multiple rows of data to return more than 1
the FROM penalties
WHERE playerno = p.playerno);
statement example: those who get paid at least once a fine player's name and initials
statement analysis: is the result of holding the outer query to the results of the comparison in the query, if the two results match, leave As a result, otherwise the result will abandon

NOT EXISTS operator
1. The operator specialized sub-query result set to determine whether or not empty: returns true if it is empty, otherwise it returns false, with the opposite exists operator

2. When using the NOT EXISTS operator
(1) common queries to the relevant sub
(2) demand tend to have "not" keyword

Example:
the SELECT NAME, Initials
the FROM the Players the p-
the WHERE the NOT EXISTS (## if the return data is the captain, not captain of return data is not in line with the needs of
the SELECT 1
the FROM Teams is
the WHERE playerno = p.playerno);
sentences to define: to get those who are not captain the player's name and initials

Summary:
subquery first execution after the execution is completed, then perform outside, from the inside to the outside of the implementation of the structure
when the single separate comparisons = (sub-query have to be single separate)
single-line multi-column comparison time = (subqueries also have to is a single row multi-column)
filtration column (column) in (when the sub-query is a multi-line column)
filtration column (multi-column) in (when the sub-query is a plurality of rows and columns)

Guess you like

Origin blog.csdn.net/qq_43028054/article/details/93760897
Recommended