4.2.11. Scalar Subqueries
4.2.11.标量子查询
A scalar subquery is an ordinary
SELECT
query in parentheses that returns exactly one row with one
column. (See Chapter 7 for information about writing queries.) The
SELECT
query is executed and
the single returned value is used in the surrounding value expression. It is an error to use a query that
returns more than one row or more than one column as a scalar subquery. (But if, during a particular
execution, the subquery returns no rows, there is no error; the scalar result is taken to be null.) The
subquery can refer to variables from the surrounding query, which will act as constants during any
one evaluation of the subquery. See also Section 9.22 for other expressions involving subqueries.
标量子查询是一个在括号中,仅返回一列一行的普通SELECT查询。(有关如何写查询语句,请参见
第7章。)执行select查询,然后返回的单个值由外层表达式使用。在标量子查询中,返回大于一行或多于一列,会返回错误。(但是,如果在特定查询中,子查询未返回行,则不会报错;此处标量结果为null。)子查询可引用外层查询的变量,该变量会在子查询的每次执行中作为常量使用。对于子查询中其他表达式,请参见
9.22节。
For example, the following finds the largest city population in each state:
例如,如下查询返回每个州人口最多的城市:
SELECT name, (SELECT max(pop) FROM cities WHERE cities.state =
states.name)
FROM states;