Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
7.7. VALUES Lists
Prev UpChapter 7. QueriesHome Next

7.7. VALUES Lists

VALUES provides a way to generate aconstant table that can be used in a query without having to actually create and populate a table on-disk. The syntax is

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 forUNION (seeSection 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 column2UNION ALLSELECT 2, 'two'UNION ALLSELECT 3, 'three';

By default,PostgreSQL assigns the namescolumn1,column2, etc. to the columns of aVALUES 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:

=> 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:

SELECTselect_list FROMtable_expression

and can appear anywhere aSELECT can. For example, you can use it as part of aUNION, or attach asort_specification (ORDER BY,LIMIT, and/orOFFSET) to it.VALUES is most commonly used as the data source in anINSERT command, and next most commonly as a subquery.

For more information seeVALUES.


Prev Up Next
7.6. LIMIT andOFFSET Home 7.8. WITH Queries (Common Table Expressions)
epubpdf
Go to PostgreSQL 11
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp