📄 select.sgml
字号:
assign a name to a calculated column using the AS clause, e.g.: <programlisting>SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen; </programlisting></para> <para> From release 6.4 of PostgreSQL, the columns in the ORDER BY clause do not need to appear in the SELECT clause. Thus the following statement is now legal: <programlisting>SELECT name FROM distributors ORDER BY code; </programlisting></para> <para> Optionally one may add the keyword DESC (descending) or ASC (ascending) after each column name in the ORDER BY clause. If not specified, ASC is assumed by default.</para> </refsect2> <refsect2 id="R2-SQL-UNION-2"> <refsect2info> <date>1998-09-24</date> </refsect2info> <title> UNION Clause </title> <para> <synopsis><replaceable class="PARAMETER">table_query</replaceable> UNION [ ALL ]<replaceable class="PARAMETER">table_query</replaceable> [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ] </synopsis> where <replaceable class="PARAMETER">table_query</replaceable> specifies any select expression without an ORDER BY clause.</para> <para> The UNION clause allows the result to be the collection of rows returned by the queries involved. (See UNION clause). The two tables that represent the direct operands of the UNION must have the same number of columns, and corresponding columns must be of compatible data types.</para> <para> By default, the result of UNION does not contain any duplicate rows unless the ALL clause is specified.</para> <para> Multiple UNION operators in the same SELECT statement are evaluated left to right. Note that the ALL keyword is not global in nature, being applied only for the current pair of table results.</para> </refsect2> <refsect2 id="R2-SQL-INTERSECT-2"> <refsect2info> <date>1998-09-24</date> </refsect2info> <title> INTERSECT Clause </title> <para> <synopsis><replaceable class="PARAMETER">table_query</replaceable> INTERSECT<replaceable class="PARAMETER">table_query</replaceable> [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ] </synopsis> where <replaceable class="PARAMETER">table_query</replaceable> specifies any select expression without an ORDER BY clause.</para> <para> The INTERSECT clause allows the result to be all rows that are common to the involved queries. (See INTERSECT clause). The two tables that represent the direct operands of the INTERSECT must have the same number of columns, and corresponding columns must be of compatible data types.</para> <para> Multiple INTERSECT operators in the same SELECT statement are evaluated left to right. </para> </refsect2> <refsect2 id="R2-SQL-EXCEPT-2"> <refsect2info> <date>1998-09-24</date> </refsect2info> <title> EXCEPT Clause </title> <para> <synopsis><replaceable class="PARAMETER">table_query</replaceable> EXCEPT <replaceable class="PARAMETER">table_query</replaceable> [ ORDER BY <replaceable class="PARAMETER">column</replaceable> [ ASC | DESC ] [, ...] ] </synopsis> where <replaceable class="PARAMETER">table_query</replaceable> specifies any select expression without an ORDER BY clause.</para> <para> The EXCEPT clause allows the result to be rows from the upper query that are not in the lower query. (See EXCEPT clause). The two tables that represent the direct operands of the EXCEPT must have the same number of columns, and corresponding columns must be of compatible data types.</para> <para> Multiple EXCEPT operators in the same SELECT statement are evaluated left to right. </para> </refsect2></refsect1> <refsect1 id="R1-SQL-SELECT-2"> <title> Usage </title> <para> To join the table <literal>films</literal> with the table <literal>distributors</literal>: </para> <programlisting>SELECT f.title, f.did, d.name, f.date_prod, f.kind FROM distributors d, films f WHERE f.did = d.didtitle |did|name | date_prod|kind-------------------------+---+----------------+----------+----------The Third Man |101|British Lion |1949-12-23|DramaThe African Queen |101|British Lion |1951-08-11|RomanticUne Femme est une Femme |102|Jean Luc Godard |1961-03-12|RomanticVertigo |103|Paramount |1958-11-14|ActionBecket |103|Paramount |1964-02-03|Drama48 Hrs |103|Paramount |1982-10-22|ActionWar and Peace |104|Mosfilm |1967-02-12|DramaWest Side Story |105|United Artists |1961-01-03|MusicalBananas |105|United Artists |1971-07-13|ComedyYojimbo |106|Toho |1961-06-16|DramaThere's a Girl in my Soup|107|Columbia |1970-06-11|ComedyTaxi Driver |107|Columbia |1975-05-15|ActionAbsence of Malice |107|Columbia |1981-11-15|ActionStoria di una donna |108|Westward |1970-08-15|RomanticThe King and I |109|20th Century Fox|1956-08-11|MusicalDas Boot |110|Bavaria Atelier |1981-11-11|DramaBed Knobs and Broomsticks|111|Walt Disney | |Musical </programlisting> <para> To sum the column <literal>len</literal> of all films and group the results by <literal>kind</literal>: </para> <programlisting>SELECT kind, SUM(len) AS total FROM films GROUP BY kind; kind |total ----------+------ Action | 07:34 Comedy | 02:58 Drama | 14:28 Musical | 06:42 Romantic | 04:38 </programlisting> <para> To sum the column <literal>len</literal> of all films, group the results by <literal>kind</literal> and show those group totals that are less than 5 hours: </para> <programlisting>SELECT kind, SUM(len) AS total FROM films GROUP BY kind HAVING SUM(len) < INTERVAL '5 hour'; kind |total ----------+------ Comedy | 02:58 Romantic | 04:38 </programlisting> <para> The following two examples are identical ways of sorting the individual results according to the contents of the second column (<literal>name</literal>): </para> <programlisting>SELECT * FROM distributors ORDER BY name;SELECT * FROM distributors ORDER BY 2; did|name ---+---------------- 109|20th Century Fox 110|Bavaria Atelier 101|British Lion 107|Columbia 102|Jean Luc Godard 113|Luso films 104|Mosfilm 103|Paramount 106|Toho 105|United Artists 111|Walt Disney 112|Warner Bros. 108|Westward </programlisting> <para> This example shows how to obtain the union of the tables <literal>distributors</literal> and <literal>actors</literal>, restricting the results to those that begin with letter W in each table. Only distinct rows are to be used, so the ALL keyword is omitted: </para> <programlisting> -- distributors: actors: -- did|name id|name -- ---+------------ --+-------------- -- 108|Westward 1|Woody Allen -- 111|Walt Disney 2|Warren Beatty -- 112|Warner Bros. 3|Walter Matthau -- ... ...SELECT distributors.name FROM distributors WHERE distributors.name LIKE 'W%'UNIONSELECT actors.name FROM actors WHERE actors.name LIKE 'W%'name--------------Walt DisneyWalter MatthauWarner Bros.Warren BeattyWestwardWoody Allen </programlisting> </refsect1> <refsect1 id="R1-SQL-SELECT-3"> <title> Compatibility </title> <para> </para> <refsect2 id="R2-SQL-SELECT-4"> <refsect2info> <date>1998-09-24</date> </refsect2info> <title> <acronym>Extensions</acronym> </title> <para><productname>Postgres</productname> allows one to omit the <command>FROM</command> clause from a query. This featurewas retained from the original PostQuel query language: <programlisting>SELECT distributors.* WHERE name = 'Westwood'; did|name ---+---------------- 108|Westward </programlisting> </para> </refsect2> <refsect2 id="R2-SQL-SELECT-5"> <refsect2info> <date>1998-09-24</date> </refsect2info> <title> <acronym>SQL92</acronym> </title> <para> </para> <refsect3 id="R3-SQL-SELECT-1"> <refsect3info> <date>1998-04-15</date> </refsect3info> <title> SELECT Clause </title> <para> In the <acronym>SQL92</acronym> standard, the optional keyword "AS" is just noise and can be omitted without affecting the meaning. The <productname>Postgres</productname> parser requires this keyword when renaming columns because the type extensibility features lead to parsing ambiguities in this context.</para> <para> In the <acronym>SQL92</acronym> standard, the new column name specified in an "AS" clause may be referenced in GROUP BY and HAVING clauses. This is not currently allowed in <productname>Postgres</productname>. </para> <para> The DISTINCT ON phrase is not part of <acronym>SQL92</acronym>. </para> </refsect3> <refsect3 id="R3-SQL-UNION-1"> <refsect3info> <date>1998-09-24</date> </refsect3info> <title> UNION Clause </title> <para> The <acronym>SQL92</acronym> syntax for UNION allows an additional CORRESPONDING BY clause: <synopsis> <replaceable class="PARAMETER">table_query</replaceable> UNION [ALL] [CORRESPONDING [BY (<replaceable class="PARAMETER">column</replaceable> [,...])]] <replaceable class="PARAMETER">table_query</replaceable> </synopsis></para> <para> The CORRESPONDING BY clause is not supported by <productname>Postgres</productname>. </para> </refsect3> </refsect2> </refsect1></refentry><!-- Keep this comment at the end of the fileLocal variables:mode: sgmlsgml-omittag:sgml-shorttag:tsgml-minimize-attributes:nilsgml-always-quote-attributes:tsgml-indent-step:1sgml-indent-data:tsgml-parent-document:nilsgml-default-dtd-file:"../reference.ced"sgml-exposed-tags:nilsgml-local-catalogs:"/usr/lib/sgml/catalog"sgml-local-ecat-files:nilEnd:-->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -