📄 select.sgml
字号:
<literal>JOIN</literal> binds more tightly than the commas separating <literal>FROM</> items. </para> <para> <literal>CROSS JOIN</> and <literal>INNER JOIN</literal> produce a simple Cartesian product, the same result as you get from listing the two items at the top level of <literal>FROM</>, but restricted by the join condition (if any). <literal>CROSS JOIN</> is equivalent to <literal>INNER JOIN ON (TRUE)</>, that is, no rows are removed by qualification. These join types are just a notational convenience, since they do nothing you couldn't do with plain <literal>FROM</> and <literal>WHERE</>. </para> <para> <literal>LEFT OUTER JOIN</> returns all rows in the qualified Cartesian product (i.e., all combined rows that pass its join condition), plus one copy of each row in the left-hand table for which there was no right-hand row that passed the join condition. This left-hand row is extended to the full width of the joined table by inserting null values for the right-hand columns. Note that only the <literal>JOIN</> clause's own condition is considered while deciding which rows have matches. Outer conditions are applied afterwards. </para> <para> Conversely, <literal>RIGHT OUTER JOIN</> returns all the joined rows, plus one row for each unmatched right-hand row (extended with nulls on the left). This is just a notational convenience, since you could convert it to a <literal>LEFT OUTER JOIN</> by switching the left and right inputs. </para> <para> <literal>FULL OUTER JOIN</> returns all the joined rows, plus one row for each unmatched left-hand row (extended with nulls on the right), plus one row for each unmatched right-hand row (extended with nulls on the left). </para> </listitem> </varlistentry> <varlistentry> <term><literal>ON <replaceable class="parameter">join_condition</replaceable></literal></term> <listitem> <para> <replaceable class="parameter">join_condition</replaceable> is an expression resulting in a value of type <type>boolean</type> (similar to a <literal>WHERE</literal> clause) that specifies which rows in a join are considered to match. </para> </listitem> </varlistentry> <varlistentry> <term><literal>USING ( <replaceable class="parameter">join_column</replaceable> [, ...] )</literal></term> <listitem> <para> A clause of the form <literal>USING ( a, b, ... )</literal> is shorthand for <literal>ON left_table.a = right_table.a AND left_table.b = right_table.b ...</literal>. Also, <literal>USING</> implies that only one of each pair of equivalent columns will be included in the join output, not both. </para> </listitem> </varlistentry> <varlistentry> <term><literal>NATURAL</literal></term> <listitem> <para> <literal>NATURAL</literal> is shorthand for a <literal>USING</> list that mentions all columns in the two tables that have the same names. </para> </listitem> </varlistentry> </variablelist> </para> </refsect2> <refsect2 id="SQL-WHERE"> <title id="sql-where-title"><literal>WHERE</literal> Clause</title> <para> The optional <literal>WHERE</literal> clause has the general form<synopsis>WHERE <replaceable class="parameter">condition</replaceable></synopsis> where <replaceable class="parameter">condition</replaceable> is any expression that evaluates to a result of type <type>boolean</type>. Any row that does not satisfy this condition will be eliminated from the output. A row satisfies the condition if it returns true when the actual row values are substituted for any variable references. </para> </refsect2> <refsect2 id="SQL-GROUPBY"> <title id="sql-groupby-title"><literal>GROUP BY</literal> Clause</title> <para> The optional <literal>GROUP BY</literal> clause has the general form<synopsis>GROUP BY <replaceable class="parameter">expression</replaceable> [, ...]</synopsis> </para> <para> <literal>GROUP BY</literal> will condense into a single row all selected rows that share the same values for the grouped expressions. <replaceable class="parameter">expression</replaceable> can be an input column name, or the name or ordinal number of an output column (<command>SELECT</command> list item), or an arbitrary expression formed from input-column values. In case of ambiguity, a <literal>GROUP BY</literal> name will be interpreted as an input-column name rather than an output column name. </para> <para> Aggregate functions, if any are used, are computed across all rows making up each group, producing a separate value for each group (whereas without <literal>GROUP BY</literal>, an aggregate produces a single value computed across all the selected rows). When <literal>GROUP BY</literal> is present, it is not valid for the <command>SELECT</command> list expressions to refer to ungrouped columns except within aggregate functions, since there would be more than one possible value to return for an ungrouped column. </para> </refsect2> <refsect2 id="SQL-HAVING"> <title id="sql-having-title"><literal>HAVING</literal> Clause</title> <para> The optional <literal>HAVING</literal> clause has the general form<synopsis>HAVING <replaceable class="parameter">condition</replaceable></synopsis> where <replaceable class="parameter">condition</replaceable> is the same as specified for the <literal>WHERE</literal> clause. </para> <para> <literal>HAVING</literal> eliminates group rows that do not satisfy the condition. <literal>HAVING</literal> is different from <literal>WHERE</literal>: <literal>WHERE</literal> filters individual rows before the application of <literal>GROUP BY</literal>, while <literal>HAVING</literal> filters group rows created by <literal>GROUP BY</literal>. Each column referenced in <replaceable class="parameter">condition</replaceable> must unambiguously reference a grouping column, unless the reference appears within an aggregate function. </para> <para> The presence of <literal>HAVING</literal> turns a query into a grouped query even if there is no <literal>GROUP BY</> clause. This is the same as what happens when the query contains aggregate functions but no <literal>GROUP BY</> clause. All the selected rows are considered to form a single group, and the <command>SELECT</command> list and <literal>HAVING</literal> clause can only reference table columns from within aggregate functions. Such a query will emit a single row if the <literal>HAVING</literal> condition is true, zero rows if it is not true. </para> </refsect2> <refsect2 id="sql-select-list"> <title id="sql-select-list-title"><command>SELECT</command> List</title> <para> The <command>SELECT</command> list (between the key words <literal>SELECT</> and <literal>FROM</>) specifies expressions that form the output rows of the <command>SELECT</command> statement. The expressions can (and usually do) refer to columns computed in the <literal>FROM</> clause. Using the clause <literal>AS <replaceable class="parameter">output_name</replaceable></literal>, another name can be specified for an output column. This name is primarily used to label the column for display. It can also be used to refer to the column's value in <literal>ORDER BY</> and <literal>GROUP BY</> clauses, but not in the <literal>WHERE</> or <literal>HAVING</> clauses; there you must write out the expression instead. </para> <para> Instead of an expression, <literal>*</literal> can be written in the output list as a shorthand for all the columns of the selected rows. Also, one can write <literal><replaceable class="parameter">table_name</replaceable>.*</literal> as a shorthand for the columns coming from just that table. </para> </refsect2> <refsect2 id="SQL-UNION"> <title id="sql-union-title"><literal>UNION</literal> Clause</title> <para> The <literal>UNION</literal> clause has this general form:<synopsis><replaceable class="parameter">select_statement</replaceable> UNION [ ALL ] <replaceable class="parameter">select_statement</replaceable></synopsis> <replaceable class="parameter">select_statement</replaceable> is any <command>SELECT</command> statement without an <literal>ORDER BY</>, <literal>LIMIT</>, <literal>FOR UPDATE</literal>, or <literal>FOR SHARE</literal> clause. (<literal>ORDER BY</> and <literal>LIMIT</> can be attached to a subexpression if it is enclosed in parentheses. Without parentheses, these clauses will be taken to apply to the result of the <literal>UNION</literal>, not to its right-hand input expression.) </para> <para> The <literal>UNION</literal> operator computes the set union of the rows returned by the involved <command>SELECT</command> statements. A row is in the set union of two result sets if it appears in at least one of the result sets. The two <command>SELECT</command> statements that represent the direct operands of the <literal>UNION</literal> must produce the same number of columns, and corresponding columns must be of compatible data types. </para> <para> The result of <literal>UNION</> does not contain any duplicate rows unless the <literal>ALL</> option is specified. <literal>ALL</> prevents elimination of duplicates. (Therefore, <literal>UNION ALL</> is usually significantly quicker than <literal>UNION</>; use <literal>ALL</> when you can.) </para> <para> Multiple <literal>UNION</> operators in the same <command>SELECT</command> statement are evaluated left to right, unless otherwise indicated by parentheses. </para> <para> Currently, <literal>FOR UPDATE</> and <literal>FOR SHARE</> cannot be specified either for a <literal>UNION</> result or for any input of a <literal>UNION</>. </para> </refsect2> <refsect2 id="SQL-INTERSECT"> <title id="sql-intersect-title"><literal>INTERSECT</literal> Clause</title> <para> The <literal>INTERSECT</literal> clause has this general form:<synopsis><replaceable class="parameter">select_statement</replaceable> INTERSECT [ ALL ] <replaceable class="parameter">select_statement</replaceable></synopsis> <replaceable class="parameter">select_statement</replaceable> is any <command>SELECT</command> statement without an <literal>ORDER BY</>, <literal>LIMIT</>, <literal>FOR UPDATE</literal>, or <literal>FOR SHARE</literal> clause. </para> <para> The <literal>INTERSECT</literal> operator computes the set intersection of the rows returned by the involved <command>SELECT</command> statements. A row is in the intersection of two result sets if it appears in both result sets. </para> <para> The result of <literal>INTERSECT</literal> does not contain any duplicate rows unless the <literal>ALL</> option is specified. With <literal>ALL</>, a row that has <replaceable>m</> duplicates in the left table and <replaceable>n</> duplicates in the right table will appear min(<replaceable>m</>,<replaceable>n</>) times in the result set. </para> <para> Multiple <literal>INTERSECT</literal> operators in the same <command>SELECT</command> statement are evaluated left to right, unless parentheses dictate otherwise. <literal>INTERSECT</literal> binds more tightly than <literal>UNION</literal>. That is, <literal>A UNION B INTERSECT C</literal> will be read as <literal>A UNION (B INTERSECT C)</literal>. </para> <para> Currently, <literal>FOR UPDATE</> and <literal>FOR SHARE</> cannot be specified either for an <literal>INTERSECT</> result or for any input of an <literal>INTERSECT</>. </para>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -