7.7. VALUES Lists

7.7. VALUES Lists
7.7.VALUES列表
VALUES provides a way to generate a “constant table” that can be used in a query without having to  actually create and populate a table on-disk. The syntax is
VALUES提供了一种生成 可在查询中使用的 “常量表”的方法,而无需实际在磁盘上创建和填充表。 语法:
 
VALUES ( expression [, ...] ) [, ...]
 
Each parenthesized list of expressions generates a row in the table. The lists must all have the same  number of elements (i.e., the number of columns in the table), and corresponding entries in each  list must have compatible data types. The actual data type assigned to each column of the result is  determined using the same rules as for UNION (see Section 10.5).
每个括号内的表达式产生一行。列表必须具有相同数量的元素(例如,表中的列数量),且对应的列具有兼容的数据类型。为每个列分配的实际数据类型规则与UNION相同(参见 10.5节)。
 
As an example:
示例:
 
VALUES (1, 'one'), (2, 'two'), (3, 'three');
 
will return a table of two columns and three rows. It's effectively equivalent to:
上例会返回一个具有两列三行的表。与以下语句等效:
 
SELECT 1 AS column1, 'one' AS column2
UNION ALL
SELECT 2, 'two'
UNION ALL
SELECT 3, 'three';
 
By default, PostgreSQL assigns the names column1 , column2 , etc. to the columns of a VALUES  table. The column names are not specified by the SQL standard and different database systems do it  differently, so it's usually better to override the default names with a table alias list, like this:
默认,PostgreSQL为VALUES表的列分配列名column1,column2等等。列名未受SQL标准约束,且不同的数据库系统可能也不同,所以,最好使用表别名列表重写默认名称,例如:
 
=> SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);
num | letter
-----+--------
1 | one
2 | two
3 | three
(3 rows)
 
Syntactically, VALUES followed by expression lists is treated as equivalent to:
在语法上,后跟表达式列表的VALUES被视为等同于:
 
SELECT select_list FROM table_expression
 
and can appear anywhere a SELECT can. For example, you can use it as part of a UNION , or attach a  sort_specification ( ORDER BY , LIMIT , and/or OFFSET ) to it. VALUES is most commonly  used as the data source in an INSERT command, and next most commonly as a subquery.
而且可出现在任何SEELCT可以出现的地方。例如,  可以将其用作UNION的一部分,也可以将sort_specification(ORDER BY,LIMIT和/或OFFSET)附加至其后。VALUES最常在INSERT命令中用作数据源,其次最常用作子查询。
 
For more information see VALUES.
更多信息,参见 VALUES
发布了341 篇原创文章 · 获赞 54 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104552332
7.7