眩晕症吃什么药最好| 养胃喝什么茶好| 遨游是什么意思| 芒果不能和什么食物一起吃| 罗布麻是什么东西| 水粉是什么| 嗓子疼吃什么药见效最快| 杏仁有什么营养| 乾隆为什么不喜欢雍正| 离婚都需要什么手续和证件| 一月20号是什么星座| 七喜是什么饮料| 杰克琼斯属于什么档次| 七月是什么季节| 男人喝什么酒壮阳最快| 梦到自己的妈妈死了是什么意思| 中国的国球是什么球| 自卑的人有什么表现| 午夜凶铃讲的是什么故事| 来月经有血块是什么原因| 做梦梦到钓鱼是什么意思| 主意正是什么意思| 樵夫是什么意思| 长期贫血对身体有什么危害| 龙和什么属相相克| hpv检查是什么| 大米粉做什么好吃| 血小板低吃什么食物补得快| 化疗是什么| 房性逸搏心律是什么意思| 什么的点头| 猫藓是什么| 扶乩是什么意思| 胃疼是什么感觉| 身心合一是什么意思| 女人乳房疼是什么原因| 什么原因引起高血压| 宫颈炎是什么病| 什么事每人每天都必须认真的做| 胃暖气是什么症状| 碱性磷酸酶偏高吃什么能降下来呢| 恍惚是什么意思| 昆仑玉什么颜色最贵| 尿隐血是什么意思| 脾胃虚寒吃什么| 银屑病是什么症状| 什么生辉| 不带壳的蜗牛叫什么| 五十知天命是什么意思| 五字五行属什么| 抽筋是缺什么| 大大是什么意思| 隐翅虫长什么样子| 什么去火效果最好| 手小的男人代表什么| 梦到发洪水是什么征兆| 脑震荡后眩晕吃什么药| 身上老是痒是什么原因| 反驳是什么意思| 普乐安片治什么病| 猪沙肝是什么部位| 孔雀的尾巴像什么| 睡眠瘫痪症是什么| 医院附近适合做什么生意| 毛发旺盛女生什么原因引起的| 蜗牛是什么动物| 特发性震颤吃什么药| 上眼皮有痣代表什么| 什么是余额宝| 什么药对伤口愈合快| 荨麻疹长什么样图片| 什么是热射病| 梦到掉头发是什么意思| 铅中毒什么症状| 鸡同鸭讲是什么意思| neg是什么意思| 为什么有些人特别招蚊子| 心电监护pr是什么意思| nba常规赛什么时候开始| 总恶心是什么病的前兆| 吃什么精力旺盛有精神| 头皮脂溢性皮炎用什么洗发水| 上火牙龈肿痛吃什么药| 什么笔不能写字| 黄油是用什么做的| 66是什么意思| 什么app可以买烟| 什么是姜黄| 右边腰疼是什么原因| 淡紫色配什么颜色好看| 静脉曲张什么症状| 2月4日什么星座| 卖萌什么意思| 失落感是什么意思| 五月十四号是什么情人节| 男生什么时候会有生理反应| 97属什么| 红细胞压积是什么意思| 孕妇吃什么蔬菜好| k金是什么| 打更的人叫什么| 宝宝囟门什么时候闭合| 跑完步想吐是什么原因| 什么是安全| 什么是质子重离子治疗| 牛肉不能和什么水果一起吃| 马来西亚说什么语言| doosan挖掘机是什么牌子| 中国国酒是什么| 66年属马的是什么命| 梦见把蛇打死了是什么意思| 翼字五行属什么| 8月24号是什么星座| 脚崴了吃什么药| 牙出血是什么病的前兆| 3475是什么罩杯| 结膜炎挂什么科| 人流后吃什么| 汤姆是什么品种的猫| 巧克力和什么不能一起吃| 梦见磨面粉是什么意思| 电轴左偏是什么原因| 香港商务签证需要什么条件| 一岁半宝宝反复发烧是什么原因| 中暑吃什么| 中午一点是什么时辰| 什么醒酒最快| 徒孙是什么意思| 仰面朝天是什么生肖| 挂失补办身份证需要什么| 什么是螨虫| 下午3点到4点是什么时辰| 纾是什么意思| 梦到头上长虱子什么意思| 眼前的苟且是什么意思| 什么色什么流| 杏林春暖的杏林指什么| kor是什么意思| 诞辰是什么意思| thc是什么费用| 榆木脑袋是什么意思| 肌无力挂什么科| 左手尾戒什么意思| 伤口感染用什么药| 降压药有什么副作用| 尿液有白色絮状物是什么原因| 木字旁加差是什么字| 8月29日什么星座| 海藻面膜有什么作用| 木甚念什么| 翡翠和和田玉有什么区别| 处女是什么意思| 什么酒不能喝打一生肖| ut是什么| 白癜风是什么引起的| 恩赐是什么意思| 舀水是什么意思| 官运是什么意思| 腾云驾雾是什么生肖| 姓林的女孩取什么名字好| 孕妇梦到被蛇咬是什么意思| 蛇蛋是什么样子的| 黄疸是什么意思| 共工是什么神| 宗师是什么意思| 从容的反义词是什么| 白色五行属什么| 2157是什么意思| 什么的木筏| 11月份是什么星座的| 怀孕初期吃什么| 什么一惊| 什么叫免疫治疗| 副支队长是什么级别| 血气分析是检查什么的| 羊水指数是什么意思| 梦见亲嘴是什么意思| 肾不好会有什么症状| et什么意思| 什么水果补钾| 强高是什么意思| 嘴角烂了涂什么药| 地中海贫血是什么意思| 甲减是什么原因引起的| 小蛮腰是什么意思| 睡觉尿多是什么原因| 秋天开什么花| 耘是什么意思| 手指缝脱皮是什么原因| 脖子落枕挂什么科| 温水煮青蛙是什么意思| 例假期间吃什么减肥| 细胞角蛋白19片段是什么意思| 红景天有什么功效| 云南的特产是什么| 问其故的故是什么意思| 三点水加个真念什么| 震动棒是什么| 南辕北辙告诉我们什么道理| 什么是放疗治疗| 三角梅什么时候换盆| 中秋节干什么| 早泄吃什么中药| 大便颗粒状是什么原因造成的| 蒲地蓝消炎片主治什么| 云字属于五行属什么| 台风什么时候走| 自性是什么意思| 窝沟封闭是什么| 特诊科是什么意思| 什么牌子的氨基酸洗面奶好| gcp是什么意思| 甲肝抗体阳性代表什么| 尾巴骨疼挂什么科| 云南白药里面的保险子有什么用| 男蛇配什么属相最好| 峦读什么| 慢性气管炎吃什么药最有效| 澳大利亚属于什么气候| 饱胀是什么意思| 黑卡是什么卡| 七月份适合种什么菜| candies什么意思| 惟妙惟肖是什么意思| 做脑ct挂什么科| 口蘑不能和什么一起吃| 金可以组什么词| 萤火虫为什么会发光| 补气血吃什么中成药最好| 南极为什么比北极冷| 优甲乐过量有什么症状| 月经期适合吃什么食物| 县人大主任是什么级别| 中指和无名指发麻是什么原因| 睾丸炎用什么药| hivab是什么检测| 吃什么能消除囊肿| 血压偏低吃什么| 脾虚吃什么水果| 低压高用什么药| 与其让你在我怀中枯萎是什么歌| 夏至要吃什么| 女人最大的底气是什么| 心脏斑块是什么意思啊| 为什么一来月经就拉肚子| 胃疼胃胀用什么药效果最好| 750是什么意思| 小儿消化不良吃什么药最好| 斯里兰卡用什么货币| 天蝎男和什么星座最配| 大学挂科是什么意思| 杀生电影讲的什么意思| 小孩内热吃什么药| us是什么意思| va是什么车牌| 棋字五行属什么| ads是什么| 汶字五行属什么| 天丝棉是什么面料| 自来熟是什么意思| 什么是伴手礼| 为什么海螺里有大海的声音| 胃热吃什么| 白细胞减少是什么原因| 百度
Supported Versions: Current (17) / 16 / 15 / 14 / 13
Development Versions: 18 / devel
Unsupported versions: 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0 / 8.4 / 8.3 / 8.2 / 8.1 / 8.0 / 7.4 / 7.3 / 7.2 / 7.1
This documentation is for an unsupported version of PostgreSQL.
You may want to view the same page for the current version, or one of the other supported versions listed above instead.

与星座匹配的理想旅游地 快来对号入座吧

Name

SELECT -- retrieve rows from a table or view

Synopsis

百度 食品药品监管总局认真研究并采纳相关意见建议。
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    * | expression [ AS output_name ] [, ...]
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition [, ...] ]
    [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
    [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
    [ LIMIT { count | ALL } ]
    [ OFFSET start ]
    [ FOR UPDATE [ OF table_name [, ...] ] ]

where from_item can be one of:

    [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ]
    ( select ) [ AS ] alias [ ( column_alias [, ...] ) ]
    function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ]
    function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] )
    from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ]

Description

SELECT retrieves rows from one or more tables. The general processing of SELECT is as follows:

  1. All elements in the FROM list are computed. (Each element in the FROM list is a real or virtual table.) If more than one element is specified in the FROM list, they are cross-joined together. (See FROM Clause below.)

  2. If the WHERE clause is specified, all rows that do not satisfy the condition are eliminated from the output. (See WHERE Clause below.)

  3. If the GROUP BY clause is specified, the output is divided into groups of rows that match on one or more values. If the HAVING clause is present, it eliminates groups that do not satisfy the given condition. (See GROUP BY Clause and HAVING Clause below.)

  4. Using the operators UNION, INTERSECT, and EXCEPT, the output of more than one SELECT statement can be combined to form a single result set. The UNION operator returns all rows that are in one or both of the result sets. The INTERSECT operator returns all rows that are strictly in both result sets. The EXCEPT operator returns the rows that are in the first result set but not in the second. In all three cases, duplicate rows are eliminated unless ALL is specified. (See UNION Clause, INTERSECT Clause, and EXCEPT Clause below.)

  5. The actual output rows are computed using the SELECT output expressions for each selected row. (See SELECT List below.)

  6. If the ORDER BY clause is specified, the returned rows are sorted in the specified order. If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce. (See ORDER BY Clause below.)

  7. DISTINCT eliminates duplicate rows from the result. DISTINCT ON eliminates rows that match on all the specified expressions. ALL (the default) will return all candidate rows, including duplicates. (See DISTINCT Clause below.)

  8. If the LIMIT or OFFSET clause is specified, the SELECT statement only returns a subset of the result rows. (See LIMIT Clause below.)

  9. The FOR UPDATE clause causes the SELECT statement to lock the selected rows against concurrent updates. (See FOR UPDATE Clause below.)

You must have SELECT privilege on a table to read its values. The use of FOR UPDATE requires UPDATE privilege as well.

Parameters

FROM Clause

The FROM clause specifies one or more source tables for the SELECT. If multiple sources are specified, the result is the Cartesian product (cross join) of all the sources. But usually qualification conditions are added to restrict the returned rows to a small subset of the Cartesian product.

FROM-clause elements can contain:

table_name

The name (optionally schema-qualified) of an existing table or view. If ONLY is specified, only that table is scanned. If ONLY is not specified, the table and all its descendant tables (if any) are scanned. * can be appended to the table name to indicate that descendant tables are to be scanned, but in the current version, this is the default behavior. (In releases before 7.1, ONLY was the default behavior.) The default behavior can be modified by changing the sql_inheritance configuration option.

alias

A substitute name for the FROM item containing the alias. An alias is used for brevity or to eliminate ambiguity for self-joins (where the same table is scanned multiple times). When an alias is provided, it completely hides the actual name of the table or function; for example given FROM foo AS f, the remainder of the SELECT must refer to this FROM item as f not foo. If an alias is written, a column alias list can also be written to provide substitute names for one or more columns of the table.

select

A sub-SELECT can appear in the FROM clause. This acts as though its output were created as a temporary table for the duration of this single SELECT command. Note that the sub-SELECT must be surrounded by parentheses, and an alias must be provided for it.

function_name

Function calls can appear in the FROM clause. (This is especially useful for functions that return result sets, but any function can be used.) This acts as though its output were created as a temporary table for the duration of this single SELECT command. An alias may also be used. If an alias is written, a column alias list can also be written to provide substitute names for one or more attributes of the function's composite return type. If the function has been defined as returning the record data type, then an alias or the key word AS must be present, followed by a column definition list in the form ( column_name data_type [, ... ] ). The column definition list must match the actual number and types of columns returned by the function.

join_type

One of

  • [ INNER ] JOIN

  • LEFT [ OUTER ] JOIN

  • RIGHT [ OUTER ] JOIN

  • FULL [ OUTER ] JOIN

  • CROSS JOIN

For the INNER and OUTER join types, a join condition must be specified, namely exactly one of NATURAL, ON join_condition, or USING (join_column [, ...]). See below for the meaning. For CROSS JOIN, none of these clauses may appear.

A JOIN clause combines two FROM items. Use parentheses if necessary to determine the order of nesting. In the absence of parentheses, JOINs nest left-to-right. In any case JOIN binds more tightly than the commas separating FROM items.

CROSS JOIN and INNER JOIN produce a simple Cartesian product, the same result as you get from listing the two items at the top level of FROM, but restricted by the join condition (if any). CROSS JOIN is equivalent to 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 FROM and WHERE.

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 JOIN clause's own condition is considered while deciding which rows have matches. Outer conditions are applied afterwards.

Conversely, 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 LEFT OUTER JOIN by switching the left and right inputs.

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).

ON join_condition

join_condition is an expression resulting in a value of type boolean (similar to a WHERE clause) that specifies which rows in a join are considered to match.

USING (join_column [, ...])

A clause of the form USING ( a, b, ... ) is shorthand for ON left_table.a = right_table.a AND left_table.b = right_table.b .... Also, USING implies that only one of each pair of equivalent columns will be included in the join output, not both.

NATURAL

NATURAL is shorthand for a USING list that mentions all columns in the two tables that have the same names.

WHERE Clause

The optional WHERE clause has the general form

WHERE condition

where condition is any expression that evaluates to a result of type boolean. 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.

GROUP BY Clause

The optional GROUP BY clause has the general form

GROUP BY expression [, ...]

GROUP BY will condense into a single row all selected rows that share the same values for the grouped expressions. expression can be an input column name, or the name or ordinal number of an output column (SELECT list item), or an arbitrary expression formed from input-column values. In case of ambiguity, a GROUP BY name will be interpreted as an input-column name rather than an output column name.

Aggregate functions, if any are used, are computed across all rows making up each group, producing a separate value for each group (whereas without GROUP BY, an aggregate produces a single value computed across all the selected rows). When GROUP BY is present, it is not valid for the SELECT 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.

HAVING Clause

The optional HAVING clause has the general form

HAVING condition

where condition is the same as specified for the WHERE clause.

HAVING eliminates group rows that do not satisfy the condition. HAVING is different from WHERE: WHERE filters individual rows before the application of GROUP BY, while HAVING filters group rows created by GROUP BY. Each column referenced in condition must unambiguously reference a grouping column, unless the reference appears within an aggregate function.

UNION Clause

The UNION clause has this general form:

select_statement UNION [ ALL ] select_statement

select_statement is any SELECT statement without an ORDER BY, LIMIT, or FOR UPDATE clause. (ORDER BY and 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 UNION, not to its right-hand input expression.)

The UNION operator computes the set union of the rows returned by the involved SELECT 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 SELECT statements that represent the direct operands of the UNION must produce the same number of columns, and corresponding columns must be of compatible data types.

The result of UNION does not contain any duplicate rows unless the ALL option is specified. ALL prevents elimination of duplicates.

Multiple UNION operators in the same SELECT statement are evaluated left to right, unless otherwise indicated by parentheses.

Currently, FOR UPDATE may not be specified either for a UNION result or for any input of a UNION.

INTERSECT Clause

The INTERSECT clause has this general form:

select_statement INTERSECT [ ALL ] select_statement

select_statement is any SELECT statement without an ORDER BY, LIMIT, or FOR UPDATE clause.

The INTERSECT operator computes the set intersection of the rows returned by the involved SELECT statements. A row is in the intersection of two result sets if it appears in both result sets.

The result of INTERSECT does not contain any duplicate rows unless the ALL option is specified. With ALL, a row that has m duplicates in the left table and n duplicates in the right table will appear min(m,n) times in the result set.

Multiple INTERSECT operators in the same SELECT statement are evaluated left to right, unless parentheses dictate otherwise. INTERSECT binds more tightly than UNION. That is, A UNION B INTERSECT C will be read as A UNION (B INTERSECT C).

EXCEPT Clause

The EXCEPT clause has this general form:

select_statement EXCEPT [ ALL ] select_statement

select_statement is any SELECT statement without an ORDER BY, LIMIT, or FOR UPDATE clause.

The EXCEPT operator computes the set of rows that are in the result of the left SELECT statement but not in the result of the right one.

The result of EXCEPT does not contain any duplicate rows unless the ALL option is specified. With ALL, a row that has m duplicates in the left table and n duplicates in the right table will appear max(m-n,0) times in the result set.

Multiple EXCEPT operators in the same SELECT statement are evaluated left to right, unless parentheses dictate otherwise. EXCEPT binds at the same level as UNION.

SELECT List

The SELECT list (between the key words SELECT and FROM) specifies expressions that form the output rows of the SELECT statement. The expressions can (and usually do) refer to columns computed in the FROM clause. Using the clause AS output_name, 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 ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING clauses; there you must write out the expression instead.

Instead of an expression, * can be written in the output list as a shorthand for all the columns of the selected rows. Also, one can write table_name.* as a shorthand for the columns coming from just that table.

ORDER BY Clause

The optional ORDER BY clause has this general form:

ORDER BY expression [ ASC | DESC | USING operator ] [, ...]

expression can be the name or ordinal number of an output column (SELECT list item), or it can be an arbitrary expression formed from input-column values.

The ORDER BY clause causes the result rows to be sorted according to the specified expressions. If two rows are equal according to the leftmost expression, the are compared according to the next expression and so on. If they are equal according to all specified expressions, they are returned in an implementation-dependent order.

The ordinal number refers to the ordinal (left-to-right) position of the result column. This feature makes it possible to define an ordering on the basis of a column that does not have a unique name. This is never absolutely necessary because it is always possible to assign a name to a result column using the AS clause.

It is also possible to use arbitrary expressions in the ORDER BY clause, including columns that do not appear in the SELECT result list. Thus the following statement is valid:

SELECT name FROM distributors ORDER BY code;

A limitation of this feature is that an ORDER BY clause applying to the result of a UNION, INTERSECT, or EXCEPT clause may only specify an output column name or number, not an expression.

If an ORDER BY expression is a simple name that matches both a result column name and an input column name, ORDER BY will interpret it as the result column name. This is the opposite of the choice that GROUP BY will make in the same situation. This inconsistency is made to be compatible with the SQL standard.

Optionally one may add the key word ASC (ascending) or DESC (descending) after any expression in the ORDER BY clause. If not specified, ASC is assumed by default. Alternatively, a specific ordering operator name may be specified in the USING clause. ASC is usually equivalent to USING < and DESC is usually equivalent to USING >. (But the creator of a user-defined data type can define exactly what the default sort ordering is, and it might correspond to operators with other names.)

The null value sorts higher than any other value. In other words, with ascending sort order, null values sort at the end, and with descending sort order, null values sort at the beginning.

Character-string data is sorted according to the locale-specific collation order that was established when the database cluster was initialized.

LIMIT Clause

The LIMIT clause consists of two independent sub-clauses:

LIMIT { count | ALL }
OFFSET start

count specifies the maximum number of rows to return, while start specifies the number of rows to skip before starting to return rows. When both are specified, start rows are skipped before starting to count the count rows to be returned.

When using LIMIT, it is a good idea to use an ORDER BY clause that constrains the result rows into a unique order. Otherwise you will get an unpredictable subset of the query's rows---you may be asking for the tenth through twentieth rows, but tenth through twentieth in what ordering? You don't know what ordering unless you specify ORDER BY.

The query planner takes LIMIT into account when generating a query plan, so you are very likely to get different plans (yielding different row orders) depending on what you use for LIMIT and OFFSET. Thus, using different LIMIT/OFFSET values to select different subsets of a query result will give inconsistent results unless you enforce a predictable result ordering with ORDER BY. This is not a bug; it is an inherent consequence of the fact that SQL does not promise to deliver the results of a query in any particular order unless ORDER BY is used to constrain the order.

DISTINCT Clause

If DISTINCT is specified, all duplicate rows are removed from the result set (one row is kept from each group of duplicates). ALL specifies the opposite: all rows are kept; that is the default.

DISTINCT ON ( expression [, ...] ) keeps only the first row of each set of rows where the given expressions evaluate to equal. The DISTINCT ON expressions are interpreted using the same rules as for ORDER BY (see above). Note that the "first row" of each set is unpredictable unless ORDER BY is used to ensure that the desired row appears first. For example,

SELECT DISTINCT ON (location) location, time, report
    FROM weather_reports
    ORDER BY location, time DESC;

retrieves the most recent weather report for each location. But if we had not used ORDER BY to force descending order of time values for each location, we'd have gotten a report from an unpredictable time for each location.

The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s). The ORDER BY clause will normally contain additional expression(s) that determine the desired precedence of rows within each DISTINCT ON group.

FOR UPDATE Clause

The FOR UPDATE clause has this form:

FOR UPDATE [ OF table_name [, ...] ]

FOR UPDATE causes the rows retrieved by the SELECT statement to be locked as though for update. This prevents them from being modified or deleted by other transactions until the current transaction ends. That is, other transactions that attempt UPDATE, DELETE, or SELECT FOR UPDATE of these rows will be blocked until the current transaction ends. Also, if an UPDATE, DELETE, or SELECT FOR UPDATE from another transaction has already locked a selected row or rows, SELECT FOR UPDATE will wait for the other transaction to complete, and will then lock and return the updated row (or no row, if the row was deleted). For further discussion see Chapter 12.

If specific tables are named in FOR UPDATE, then only rows coming from those tables are locked; any other tables used in the SELECT are simply read as usual.

FOR UPDATE cannot be used in contexts where returned rows can't be clearly identified with individual table rows; for example it can't be used with aggregation.

FOR UPDATE may appear before LIMIT for compatibility with PostgreSQL versions before 7.3. It effectively executes after LIMIT, however, and so that is the recommended place to write it.

Examples

To join the table films with the table distributors:

SELECT f.title, f.did, d.name, f.date_prod, f.kind
    FROM distributors d, films f
    WHERE f.did = d.did

       title       | did |     name     | date_prod  |   kind
-------------------+-----+--------------+------------+----------
 The Third Man     | 101 | British Lion | 2025-08-08 | Drama
 The African Queen | 101 | British Lion | 2025-08-08 | Romantic
 ...

To sum the column len of all films and group the results by kind:

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

To sum the column len of all films, group the results by kind and show those group totals that are less than 5 hours:

SELECT kind, sum(len) AS total
    FROM films
    GROUP BY kind
    HAVING sum(len) < interval '5 hours';

   kind   | total
----------+-------
 Comedy   | 02:58
 Romantic | 04:38

The following two examples are identical ways of sorting the individual results according to the contents of the second column (name):

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

The next example shows how to obtain the union of the tables distributors and actors, restricting the results to those that begin with the letter W in each table. Only distinct rows are wanted, so the key word ALL is omitted.

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%'
UNION
SELECT actors.name
    FROM actors
    WHERE actors.name LIKE 'W%';

      name
----------------
 Walt Disney
 Walter Matthau
 Warner Bros.
 Warren Beatty
 Westward
 Woody Allen

This example shows how to use a function in the FROM clause, both with and without a column definition list:

CREATE FUNCTION distributors(int) RETURNS SETOF distributors AS '
    SELECT * FROM distributors WHERE did = $1;
' LANGUAGE SQL;

SELECT * FROM distributors(111);
 did |    name
-----+-------------
 111 | Walt Disney

CREATE FUNCTION distributors_2(int) RETURNS SETOF record AS '
    SELECT * FROM distributors WHERE did = $1;
' LANGUAGE SQL;

SELECT * FROM distributors_2(111) AS (f1 int, f2 text);
 f1  |     f2
-----+-------------
 111 | Walt Disney

Compatibility

Of course, the SELECT statement is compatible with the SQL standard. But there are some extensions and some missing features.

Omitted FROM Clauses

PostgreSQL allows one to omit the FROM clause. It has a straightforward use to compute the results of simple expressions:

SELECT 2+2;

 ?column?
----------
        4

Some other SQL databases cannot do this except by introducing a dummy one-row table from which to do the SELECT.

A less obvious use is to abbreviate a normal SELECT from tables:

SELECT distributors.* WHERE distributors.name = 'Westward';

 did |   name
-----+----------
 108 | Westward

This works because an implicit FROM item is added for each table that is referenced in other parts of the SELECT statement but not mentioned in FROM.

While this is a convenient shorthand, it's easy to misuse. For example, the command

SELECT distributors.* FROM distributors d;

is probably a mistake; most likely the user meant

SELECT d.* FROM distributors d;

rather than the unconstrained join

SELECT distributors.* FROM distributors d, distributors distributors;

that he will actually get. To help detect this sort of mistake, PostgreSQL will warn if the implicit-FROM feature is used in a SELECT statement that also contains an explicit FROM clause. Also, it is possible to disable the implicit-FROM feature by setting the ADD_MISSING_FROM parameter to false.

The AS Key Word

In the SQL standard, the optional key word AS is just noise and can be omitted without affecting the meaning. The PostgreSQL parser requires this key word when renaming output columns because the type extensibility features lead to parsing ambiguities without it. AS is optional in FROM items, however.

Namespace Available to GROUP BY and ORDER BY

In the SQL92 standard, an ORDER BY clause may only use result column names or numbers, while a GROUP BY clause may only use expressions based on input column names. PostgreSQL extends each of these clauses to allow the other choice as well (but it uses the standard's interpretation if there is ambiguity). PostgreSQL also allows both clauses to specify arbitrary expressions. Note that names appearing in an expression will always be taken as input-column names, not as result-column names.

SQL99 uses a slightly different definition which is not entirely upward compatible with SQL92. In most cases, however, PostgreSQL will interpret an ORDER BY or GROUP BY expression the same way SQL99 does.

Nonstandard Clauses

The clauses DISTINCT ON, LIMIT, and OFFSET are not defined in the SQL standard.

mmf是什么药 金钱肚是什么 猪肚搭配什么煲汤最好 颈椎病最怕干什么活 娃娃鱼吃什么食物
凝神是什么意思 rh血型阳性什么意思 cocoon是什么品牌 气管憩室什么意思 黄瓜片贴脸上有什么效果
岁月如歌下一句是什么 鳗鱼是什么鱼 族谱是什么意思 金蝉脱壳是什么意思 荔枝有什么营养
健康管理是什么 甲胎蛋白增高说明什么 什么克金 npn是什么意思 鸡炖什么好吃
海清是什么意思hcv7jop4ns5r.cn 小便发黄是什么原因引起的hcv8jop5ns8r.cn 什么水果可以美白hcv9jop1ns8r.cn 更年期综合症吃什么药hcv8jop4ns1r.cn cathy是什么意思hcv9jop2ns4r.cn
捡什么废品最值钱hcv7jop4ns8r.cn 男人是什么动物hcv8jop5ns4r.cn 脐炎用什么药hcv7jop5ns2r.cn 蟋蟀吃什么食物hcv8jop2ns4r.cn 十二生肖它第一是什么生肖hcv9jop3ns9r.cn
什么的月饼hcv8jop6ns3r.cn 肚脐下面疼是什么原因hcv8jop4ns3r.cn 睡眠不好吃什么好hcv9jop4ns1r.cn 雾化是什么意思cj623037.com 妈妈的堂哥叫什么hcv7jop5ns4r.cn
什么叫985hcv8jop8ns9r.cn 割韭菜什么意思hcv8jop7ns0r.cn 雌二醇是什么意思hcv7jop6ns4r.cn 垂体是什么意思hcv9jop0ns2r.cn 吃靶向药不能吃什么hcv9jop4ns8r.cn
百度