mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-15 08:20:16 +08:00
Minor wording and formatting cleanups.
This commit is contained in:
parent
b6bdc43522
commit
4dce924b62
@ -1,367 +1,372 @@
|
||||
<REFENTRY ID="SQL-SELECT-1">
|
||||
<REFMETA>
|
||||
<REFENTRYTITLE>
|
||||
SELECT
|
||||
</REFENTRYTITLE>
|
||||
<REFMISCINFO>SQL - Language Statements</REFMISCINFO>
|
||||
</REFMETA>
|
||||
<REFNAMEDIV>
|
||||
<REFNAME>
|
||||
SELECT
|
||||
</REFNAME>
|
||||
<REFPURPOSE>
|
||||
Retrieve rows from a table or view
|
||||
</REFPURPOSE>
|
||||
<REFSYNOPSISDIV>
|
||||
<REFSYNOPSISDIVINFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSYNOPSISDIVINFO>
|
||||
<SYNOPSIS>
|
||||
<REPLACEABLE CLASS="PARAMETER">
|
||||
</REPLACEABLE>
|
||||
<refentry id="SQL-SELECT">
|
||||
<refmeta>
|
||||
<refentrytitle>
|
||||
SELECT
|
||||
</refentrytitle>
|
||||
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
||||
</refmeta>
|
||||
<refnamediv>
|
||||
<refname>
|
||||
SELECT
|
||||
</refname>
|
||||
<refpurpose>
|
||||
Retrieve rows from a table or view.
|
||||
</refpurpose></refnamediv>
|
||||
<refsynopsisdiv>
|
||||
<refsynopsisdivinfo>
|
||||
<date>1998-09-06</date>
|
||||
</refsynopsisdivinfo>
|
||||
<synopsis>
|
||||
SELECT [ALL|DISTINCT]
|
||||
<REPLACEABLE CLASS="PARAMETER">expression</REPLACEABLE> [AS <REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE>] [, ...]
|
||||
[INTO [TABLE] <REPLACEABLE CLASS="PARAMETER">intable</REPLACEABLE>]
|
||||
[FROM <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE> [<REPLACEABLE CLASS="PARAMETER">alias</REPLACEABLE>] [, ...] ]
|
||||
[WHERE <REPLACEABLE CLASS="PARAMETER">condition</REPLACEABLE>]
|
||||
[GROUP BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [, ...] ]
|
||||
[HAVING <REPLACEABLE CLASS="PARAMETER">condition</REPLACEABLE> [, ...] ]
|
||||
[UNION [ALL] <REPLACEABLE CLASS="PARAMETER">select</REPLACEABLE>]
|
||||
[ORDER BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [ASC | DESC] [, ...] ]
|
||||
</SYNOPSIS>
|
||||
|
||||
<REFSECT2 ID="R2-SQL-SELECT-1">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSECT2INFO>
|
||||
<TITLE>
|
||||
Inputs
|
||||
</TITLE>
|
||||
|
||||
<PARA>
|
||||
<VARIABLELIST>
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">expression</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
The name of a table's column or an expression.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
Specifies another name for a column or an expression using
|
||||
the AS clause. <REPLACEABLE CLASS="PARAMETER">name</REPLACEABLE> cannot be used in the WHERE
|
||||
condition. It can, however, be referenced in associated
|
||||
ORDER BY or GROUP BY clauses.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">intable</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
If the INTO TABLE clause is specified, the result of the
|
||||
query will be stored in another table with the indicated
|
||||
name.
|
||||
If <REPLACEABLE CLASS="PARAMETER">intable</REPLACEABLE> does not exist, it will be created automatically.
|
||||
|
||||
<Note>
|
||||
<Para>
|
||||
The CREATE TABLE AS statement will also create a new table from a select query.
|
||||
</Para>
|
||||
</Note>
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
The name of an existing table referenced by FROM clause.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">alias</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
An alternate name for the preceeding <REPLACEABLE CLASS="PARAMETER">table</REPLACEABLE>.
|
||||
Used for brevity or to eliminate ambiguity for joins within a single table.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">condition</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
An expression leading to a boolean (true/false) result.
|
||||
See the WHERE clause.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
The name of a table's column.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<REPLACEABLE CLASS="PARAMETER">select</REPLACEABLE>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
A select statement with all features except the ORDER BY clause.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
</VARIABLELIST>
|
||||
</PARA>
|
||||
</REFSECT2>
|
||||
|
||||
<REFSECT2 ID="R2-SQL-SELECT-2">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSECT2INFO>
|
||||
<TITLE>
|
||||
Outputs
|
||||
</TITLE>
|
||||
<VARIABLELIST>
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
Rows
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
The complete set of rows resulting from the query specification.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
|
||||
<VARLISTENTRY>
|
||||
<TERM>
|
||||
<ReturnValue>count</ReturnValue>
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
The count of rows returned by the query.
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
</VARIABLELIST>
|
||||
|
||||
</REFSECT2>
|
||||
</REFSYNOPSISDIV>
|
||||
|
||||
<REFSECT1 ID="R1-SQL-SELECT-1">
|
||||
<REFSECT1INFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSECT1INFO>
|
||||
<TITLE>
|
||||
Description
|
||||
</TITLE>
|
||||
<PARA>
|
||||
SELECT is the principal means to access information
|
||||
in <productname>Postgres</productname>.
|
||||
|
||||
<replaceable class="PARAMETER">expression</replaceable> [AS <replaceable class="PARAMETER">name</replaceable>] [, ...]
|
||||
[INTO [TABLE] <replaceable class="PARAMETER">intable</replaceable>]
|
||||
[FROM <replaceable class="PARAMETER">table</replaceable> [<replaceable class="PARAMETER">alias</replaceable>] [, ...] ]
|
||||
[WHERE <replaceable class="PARAMETER">condition</replaceable>]
|
||||
[GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...] ]
|
||||
[HAVING <replaceable class="PARAMETER">condition</replaceable> [, ...] ]
|
||||
[UNION [ALL] <replaceable class="PARAMETER">select</replaceable>]
|
||||
[ORDER BY <replaceable class="PARAMETER">column</replaceable> [ASC | DESC] [, ...] ]
|
||||
</synopsis>
|
||||
|
||||
<refsect2 id="R2-SQL-SELECT-1">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</date>
|
||||
</refsect2info>
|
||||
<title>
|
||||
Inputs
|
||||
</title>
|
||||
|
||||
<para>
|
||||
<variablelist>
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">expression</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
The name of a table's column or an expression.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">name</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
Specifies another name for a column or an expression using
|
||||
the AS clause. <replaceable class="PARAMETER">name</replaceable>
|
||||
cannot be used in the WHERE
|
||||
condition. It can, however, be referenced in associated
|
||||
ORDER BY or GROUP BY clauses.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">intable</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
If the INTO TABLE clause is specified, the result of the
|
||||
query will be stored in another table with the indicated
|
||||
name.
|
||||
If <replaceable class="PARAMETER">intable</replaceable> does
|
||||
not exist, it will be created automatically.
|
||||
|
||||
<note>
|
||||
<para>
|
||||
The <command>CREATE TABLE AS</command> statement will also
|
||||
create a new table from a select query.
|
||||
</para>
|
||||
</note>
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">table</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
The name of an existing table referenced by the FROM clause.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">alias</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
An alternate name for the preceding
|
||||
<replaceable class="PARAMETER">table</replaceable>.
|
||||
It is used for brevity or to eliminate ambiguity for joins
|
||||
within a single table.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">condition</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
A boolean expression giving a result of true or false.
|
||||
See the WHERE clause.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">column</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
The name of a table's column.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<replaceable class="PARAMETER">select</replaceable>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
A select statement with all features except the ORDER BY clause.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
</variablelist>
|
||||
</para>
|
||||
</refsect2>
|
||||
|
||||
<refsect2 id="R2-SQL-SELECT-2">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</date>
|
||||
</refsect2info>
|
||||
<title>
|
||||
Outputs
|
||||
</title>
|
||||
<variablelist>
|
||||
<varlistentry>
|
||||
<term>
|
||||
Rows
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
The complete set of rows resulting from the query specification.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
|
||||
<varlistentry>
|
||||
<term>
|
||||
<returnvalue>count</returnvalue>
|
||||
</term>
|
||||
<listitem>
|
||||
<para>
|
||||
The count of rows returned by the query.
|
||||
</para>
|
||||
</listitem>
|
||||
</varlistentry>
|
||||
</variablelist>
|
||||
|
||||
</refsect2>
|
||||
</refsynopsisdiv>
|
||||
|
||||
<refsect1 id="R1-SQL-SELECT-1">
|
||||
<refsect1info>
|
||||
<date>1998-09-06</date>
|
||||
</refsect1info>
|
||||
<title>
|
||||
Description
|
||||
</title>
|
||||
<para>
|
||||
SELECT will get all rows which satisfy the WHERE condition
|
||||
or all rows of a table if WHERE is omitted.
|
||||
|
||||
<PARA>
|
||||
or all rows of a table if WHERE is omitted.</para>
|
||||
|
||||
<para>
|
||||
The GROUP BY clause allows a user to divide a table
|
||||
conceptually into groups. (See GROUP BY clause).
|
||||
|
||||
conceptually into groups. (See GROUP BY clause).</para>
|
||||
|
||||
<para>
|
||||
The HAVING clause specifies a grouped table derived by the
|
||||
elimination of groups from the result of the previously
|
||||
specified clause. (See HAVING clause).
|
||||
|
||||
specified clause. (See HAVING clause).</para>
|
||||
|
||||
<para>
|
||||
The ORDER BY clause allows a user to specify that he/she
|
||||
wishes the rows sorted according to the ASCending or
|
||||
DESCending mode operator. (See ORDER BY clause)
|
||||
|
||||
DESCending mode operator. (See ORDER BY clause)</para>
|
||||
|
||||
<para>
|
||||
The UNION clause specifies a table derived from a Cartesian
|
||||
product union join. (See UNION clause).
|
||||
|
||||
<PARA>
|
||||
product union join. (See UNION clause).</para>
|
||||
|
||||
<para>
|
||||
You must have SELECT privilege to a table to read its values
|
||||
(See GRANT/REVOKE statements).
|
||||
|
||||
<REFSECT2 ID="R2-SQL-WHERE-2">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSECT2INFO>
|
||||
<TITLE>
|
||||
WHERE clause
|
||||
</TITLE>
|
||||
<PARA>
|
||||
The optional WHERE condition has the general form:
|
||||
|
||||
<Synopsis>
|
||||
WHERE <REPLACEABLE CLASS="PARAMETER">expr</REPLACEABLE> <REPLACEABLE CLASS="PARAMETER">cond_op</REPLACEABLE> <REPLACEABLE CLASS="PARAMETER">expr</REPLACEABLE> [<REPLACEABLE CLASS="PARAMETER">log_op</REPLACEABLE> ...]
|
||||
</Synopsis>
|
||||
|
||||
where <REPLACEABLE CLASS="PARAMETER">cond_op</REPLACEABLE> can be one of: =, <, <=, >, >=, <>
|
||||
or a conditional operator like ALL, ANY, IN, LIKE, et cetera
|
||||
and <REPLACEABLE CLASS="PARAMETER">log_op</REPLACEABLE> can be one of: AND, OR, NOT.
|
||||
The comparison returns either TRUE or FALSE and all
|
||||
instances will be discarded
|
||||
if the expression evaluates to FALSE.
|
||||
</PARA>
|
||||
</REFSECT2>
|
||||
|
||||
<REFSECT2 ID="R2-SQL-GROUPBY-2">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSECT2INFO>
|
||||
<TITLE>
|
||||
GROUP BY clause
|
||||
</TITLE>
|
||||
<PARA>
|
||||
(See GRANT/REVOKE statements).</para>
|
||||
|
||||
<refsect2 id="R2-SQL-WHERE-2">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</date>
|
||||
</refsect2info>
|
||||
<title>
|
||||
WHERE clause
|
||||
</title>
|
||||
<para>
|
||||
The optional WHERE condition has the general form:
|
||||
|
||||
<synopsis>
|
||||
WHERE <replaceable class="PARAMETER">expr</replaceable> <replaceable class="PARAMETER">cond_op</replaceable> <replaceable class="PARAMETER">expr</replaceable> [<replaceable class="PARAMETER">log_op</replaceable> ...]
|
||||
</synopsis>
|
||||
|
||||
where <replaceable class="PARAMETER">cond_op</replaceable> can be
|
||||
one of: =, <, <=, >, >=, <>
|
||||
or a conditional operator like ALL, ANY, IN, LIKE, et cetera
|
||||
and <replaceable class="PARAMETER">log_op</replaceable> can be one
|
||||
of: AND, OR, NOT.
|
||||
The comparison returns either TRUE or FALSE and all
|
||||
instances will be discarded
|
||||
if the expression evaluates to FALSE.
|
||||
</para>
|
||||
</refsect2>
|
||||
|
||||
<refsect2 id="R2-SQL-GROUPBY-2">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</date>
|
||||
</refsect2info>
|
||||
<title>
|
||||
GROUP BY clause
|
||||
</title>
|
||||
<para>
|
||||
GROUP BY specifies a grouped table derived by the application
|
||||
of the this clause:
|
||||
<SYNOPSIS>
|
||||
GROUP BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [, ...]
|
||||
</SYNOPSIS>
|
||||
|
||||
<REFSECT2 ID="R2-SQL-HAVING-2">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSECT2INFO>
|
||||
<TITLE>
|
||||
HAVING clause
|
||||
</TITLE>
|
||||
<PARA>
|
||||
The optional HAVING condition has the general form:
|
||||
|
||||
<Synopsis>
|
||||
HAVING <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE>
|
||||
</Synopsis>
|
||||
|
||||
where <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE> is the same
|
||||
as specified for the WHERE clause.
|
||||
|
||||
<Para>
|
||||
<synopsis>
|
||||
GROUP BY <replaceable class="PARAMETER">column</replaceable> [, ...]
|
||||
</synopsis></para></refsect2>
|
||||
|
||||
<refsect2 id="R2-SQL-HAVING-2">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</date>
|
||||
</refsect2info>
|
||||
<title>
|
||||
HAVING clause
|
||||
</title>
|
||||
<para>
|
||||
The optional HAVING condition has the general form:
|
||||
|
||||
<synopsis>
|
||||
HAVING <replaceable class="PARAMETER">cond_expr</replaceable>
|
||||
</synopsis>
|
||||
|
||||
where <replaceable class="PARAMETER">cond_expr</replaceable> is the same
|
||||
as specified for the WHERE clause.</para>
|
||||
|
||||
<para>
|
||||
HAVING specifies a grouped table derived by the elimination
|
||||
of groups from the result of the previously specified clause
|
||||
that do not meet the <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE>.
|
||||
|
||||
<Para>
|
||||
Each column referenced in <REPLACEABLE CLASS="PARAMETER">cond_expr</REPLACEABLE> shall unambiguously
|
||||
that do not meet the <replaceable class="PARAMETER">cond_expr</replaceable>.</para>
|
||||
|
||||
<para>
|
||||
Each column referenced in <replaceable class="PARAMETER">cond_expr</replaceable> shall unambiguously
|
||||
reference a grouping column.
|
||||
</PARA>
|
||||
</REFSECT2>
|
||||
|
||||
<REFSECT2 ID="R2-SQL-ORDERBYCLAUSE-2">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</DATE>
|
||||
</REFSECT2INFO>
|
||||
<TITLE>
|
||||
ORDER BY clause
|
||||
</TITLE>
|
||||
<PARA>
|
||||
<Synopsis>
|
||||
ORDER BY <REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> [ASC | DESC] [, ...]
|
||||
</Synopsis>
|
||||
|
||||
<PARA>
|
||||
<REPLACEABLE CLASS="PARAMETER">column</REPLACEABLE> can be either a column
|
||||
name or an ordinal number.
|
||||
<PARA>
|
||||
</para>
|
||||
</refsect2>
|
||||
|
||||
<refsect2 id="R2-SQL-ORDERBYCLAUSE-2">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</date>
|
||||
</refsect2info>
|
||||
<title>
|
||||
ORDER BY clause
|
||||
</title>
|
||||
<para>
|
||||
<synopsis>
|
||||
ORDER BY <replaceable class="PARAMETER">column</replaceable> [ASC | DESC] [, ...]
|
||||
</synopsis></para>
|
||||
|
||||
<para>
|
||||
<replaceable class="PARAMETER">column</replaceable> can be either a column
|
||||
name or an ordinal number.</para>
|
||||
<para>
|
||||
The ordinal numbers refers to the ordinal (left-to-right) position
|
||||
of the column. This feature makes it possible to define an ordering
|
||||
on the basis of a column that does not have a proper name.
|
||||
This is never absolutely necessary because it is always possible 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>
|
||||
<programlisting>
|
||||
SELECT title, date_prod + 1 AS newlen FROM films ORDER BY newlen;
|
||||
</programlisting></para>
|
||||
|
||||
<para>
|
||||
The columns in the ORDER BY must appear in the SELECT clause.
|
||||
Thus the following statement is illegal:
|
||||
<ProgramListing>
|
||||
SELECT name FROM distributors ORDER BY code;
|
||||
</ProgramListing>
|
||||
|
||||
<PARA>
|
||||
<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.
|
||||
</REFSECT2>
|
||||
|
||||
<REFSECT2 ID="R2-SQL-UNION-2">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</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>
|
||||
The UNION operator specifies a table derived from a Cartesian product.
|
||||
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>
|
||||
By default, the result of UNION does not contain any duplicate rows
|
||||
unless the ALL clause is specified.
|
||||
|
||||
<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.
|
||||
|
||||
</REFSECT2>
|
||||
|
||||
<REFSECT1 ID="R1-SQL-SELECT-2">
|
||||
<TITLE>
|
||||
Usage
|
||||
</TITLE>
|
||||
<PARA>
|
||||
</PARA>
|
||||
<ProgramListing>
|
||||
--Join table films with their distributors:
|
||||
--
|
||||
If not specified, ASC is assumed by default.</para>
|
||||
</refsect2>
|
||||
|
||||
<refsect2 id="R2-SQL-UNION-2">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</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 operator specifies a table derived from a Cartesian product.
|
||||
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></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.did;
|
||||
WHERE f.did = d.did
|
||||
|
||||
title |did|name | date_prod|kind
|
||||
-------------------------+---+----------------+----------+----------
|
||||
@ -382,11 +387,12 @@ Usage
|
||||
The King and I |109|20th Century Fox|1956-08-11|Musical
|
||||
Das Boot |110|Bavaria Atelier |1981-11-11|Drama
|
||||
Bed Knobs and Broomsticks|111|Walt Disney | |Musical
|
||||
</ProgramListing>
|
||||
|
||||
<ProgramListing>
|
||||
--sum column "len" of all films group by "kind":
|
||||
--
|
||||
</programlisting>
|
||||
<para>
|
||||
To sum the column <literal>len</literal> of all films and group
|
||||
the reults by <literal>kind</literal>:
|
||||
</para>
|
||||
<programlisting>
|
||||
SELECT kind, SUM(len) AS total FROM films GROUP BY kind;
|
||||
|
||||
kind |total
|
||||
@ -396,12 +402,14 @@ Usage
|
||||
Drama | 14:28
|
||||
Musical | 06:42
|
||||
Romantic | 04:38
|
||||
</ProgramListing>
|
||||
</programlisting>
|
||||
|
||||
<ProgramListing>
|
||||
--sum column length of all films group by "kind"
|
||||
--having total duration < 5 hours:
|
||||
--
|
||||
<para>
|
||||
To sum the column <literal>len</literal> of all films, group
|
||||
the reults 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
|
||||
@ -411,11 +419,13 @@ Usage
|
||||
----------+------
|
||||
Comedy | 02:58
|
||||
Romantic | 04:38
|
||||
</ProgramListing>
|
||||
|
||||
<ProgramListing>
|
||||
--The following two examples are identicals:
|
||||
--
|
||||
</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;
|
||||
|
||||
@ -434,29 +444,31 @@ Usage
|
||||
111|Walt Disney
|
||||
112|Warner Bros.
|
||||
108|Westward
|
||||
</ProgramListing>
|
||||
</programlisting>
|
||||
|
||||
<ProgramListing>
|
||||
--union of table distributors and table actors:
|
||||
-- (only those that begin with letter W):
|
||||
--
|
||||
-- distributors: actors:
|
||||
<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 only distinct rows (without ALL):
|
||||
--
|
||||
|
||||
SELECT distributors.name
|
||||
FROM distributors
|
||||
WHERE distributors.name LIKE 'W%'
|
||||
UNION
|
||||
SELECT actors.name
|
||||
FROM actors
|
||||
WHERE actors.name LIKE 'W%';
|
||||
WHERE actors.name LIKE 'W%'
|
||||
|
||||
name
|
||||
--------------
|
||||
@ -466,88 +478,91 @@ Usage
|
||||
Warren Beatty
|
||||
Westward
|
||||
Woody Allen
|
||||
</ProgramListing>
|
||||
</programlisting>
|
||||
|
||||
</refsect1>
|
||||
|
||||
<refsect1 id="R1-SQL-SELECT-3">
|
||||
<title>
|
||||
Compatibility
|
||||
</title>
|
||||
<para>
|
||||
</para>
|
||||
|
||||
<refsect2 id="R2-SQL-SELECT-4">
|
||||
<refsect2info>
|
||||
<date>1998-09-06</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>
|
||||
</refsect3>
|
||||
|
||||
</REFSECT1>
|
||||
<refsect3 id="R3-SQL-UNION-1">
|
||||
<refsect3info>
|
||||
<date>1998-09-06</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>
|
||||
|
||||
<REFSECT1 ID="R1-SQL-SELECT-3">
|
||||
<TITLE>
|
||||
Compatibility
|
||||
</TITLE>
|
||||
<PARA>
|
||||
</PARA>
|
||||
<para>
|
||||
The CORRESPONDING BY clause is not supported by
|
||||
<productname>Postgres</productname>.
|
||||
</para>
|
||||
</refsect3>
|
||||
|
||||
</refsect2>
|
||||
</refsect1>
|
||||
</refentry>
|
||||
|
||||
<REFSECT2 ID="R2-SQL-SELECT-4">
|
||||
<REFSECT2INFO>
|
||||
<DATE>1998-04-15</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>
|
||||
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>
|
||||
</REFSECT3>
|
||||
|
||||
<REFSECT3 ID="R3-SQL-UNION-1">
|
||||
<REFSECT3INFO>
|
||||
<DATE>1998-04-15</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>
|
||||
The CORRESPONDING BY clause is not supported by <ProductName>Postgres</ProductName>.
|
||||
</PARA>
|
||||
</REFSECT3>
|
||||
|
||||
</REFSECT2>
|
||||
</REFSECT1>
|
||||
</REFENTRY>
|
||||
|
||||
<!--
|
||||
<REPLACEABLE CLASS="PARAMETER">
|
||||
</REPLACEABLE>
|
||||
<ReturnValue></ReturnValue>
|
||||
<PARA>
|
||||
</PARA>
|
||||
<VARIABLELIST>
|
||||
<VARLISTENTRY>
|
||||
<TERM>•
|
||||
</TERM>
|
||||
<LISTITEM>
|
||||
<PARA>
|
||||
</PARA>
|
||||
</LISTITEM>
|
||||
</VARLISTENTRY>
|
||||
</VARIABLELIST>
|
||||
<PARA>
|
||||
</PARA>
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
mode: sgml
|
||||
sgml-omittag:t
|
||||
sgml-shorttag:t
|
||||
sgml-minimize-attributes:nil
|
||||
sgml-always-quote-attributes:t
|
||||
sgml-indent-step:1
|
||||
sgml-indent-data:t
|
||||
sgml-parent-document:nil
|
||||
sgml-default-dtd-file:"../reference.ced"
|
||||
sgml-exposed-tags:nil
|
||||
sgml-local-catalogs:"/usr/lib/sgml/catalog"
|
||||
sgml-local-ecat-files:nil
|
||||
End:
|
||||
-->
|
||||
|
Loading…
Reference in New Issue
Block a user