table of Contents
The concept of subqueries
子查询(也称内查询)含义: 出现在其他语句中的select语句,称为子查询或内查询。
外部的查询语句,称为主查询或外查询。
Classification of subqueries
Classified according to where the subquery appears:
- After select:
only supports scalar subqueries - From behind:
support table subquery - After where or having:
scalar subquery (used more)
column subquery (used more)
row subquery (used less) - After exists (related subquery):
table subquery
Classified according to the number of rows and columns of the result set:
- Scalar sub query (the result set has only one row and one column)
- Column query (the result set has only one column and multiple rows)
- Row subquery (the result set has one row and multiple columns)
- Table subquery (the result set is generally multi-row and multi-column)
Classified according to where the subquery appears:
behind where or having
- 1. Scalar subquery (single row subquery)
- 2. Column sub-query (multi-line sub-query)
- 3. Row subquery (multiple columns and multiple rows)
Features:
- 1. The subquery is placed in parentheses
- 2. Subquery is generally placed on the right side of the condition
- 3. Standard sub-queries are generally used with single-line operators (> <>= <= = <>).
Lie sub-queries are generally used with multi-line operators in, any/some, all. - 4. The execution of the subquery has priority over the execution of the main query, and the conditions of the main query use the results of the subquery.
1. Scalar quantum query
非法使用标量子查询 子查询不是一行一列
2. Column query
列子查询(多行子查询): 返回多行,使用多行比较操作符。
Operator | meaning |
---|---|
IN / NOT IN | Equal to any one in the list |
ANY / some | Compare with a value returned by the subquery |
ALL | Compare with all the values returned by the subquery |
3. Row query
select behind
select后面的子查询 仅仅支持标量子查询
from behind
将子查询充当一张表,要求必须起别名。
behind exists