11---
22title : VALUES
33---
4-
54import FunctionDescription from '@site/src /components/FunctionDescription';
65
76<FunctionDescription description =" Introduced or updated: v1.2.65 " />
87
9- VALUES 子句用于显式定义一组行,以便在查询中使用。它允许您提供一个值列表,这些值可以用作 SQL 语句中的临时表 。
8+ VALUES 子句通过显式定义数据行来创建内联表。这个临时表可以直接使用,也可以在其他 SQL 语句中使用 。
109
1110## 语法
1211
1312``` sql
14- VALUES (value_1_1, value_1_2, ...), (value_2_1, value_2_2, ...), ...
13+ SELECT ...
14+ FROM ( VALUES ( < expr> [ , < expr> [ , ... ] ] ) [ , ( ... ) ] ) [ [ AS ] < table_alias> [ ( < column_alias> [, ... ] ) ] ]
15+ [ ... ]
1516```
1617
17- - VALUES 子句后跟用括号括起来的值集。
18- - 每个值集表示要插入到临时表中的一行。
19- - 在每个值集中,各个值以逗号分隔,并对应于临时表的列。
20- - 当您插入多行而不指定列名时,Databend 会自动分配默认列名,如 _ col0_ 、_ col1_ 、_ col2_ 等。
18+ ** 要点:**
19+ - 当 VALUES 子句在 FROM 子句中使用时,必须用括号括起来:` FROM (VALUES ...) `
20+ - 每个带括号的表达式组代表一行
21+ - 列名自动分配为 ** col0** 、** col1** 等(从零开始的索引)
22+ - 您可以使用表别名提供自定义列名
2123
2224## 示例
2325
24- 这些示例演示了如何使用 VALUES 子句以各种格式显示城市数据:直接显示或按人口排序:
26+ ### 基本用法
2527
2628``` sql
27- -- 直接返回数据
29+ -- 直接使用,自动生成列名 (col0, col1)
2830VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 );
2931
3032col0 |col1 |
@@ -33,7 +35,7 @@ Toronto |2731571|
3335Vancouver| 631486 |
3436Montreal |1704694 |
3537
36- -- 排序数据
38+ -- 使用 ORDER BY
3739VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 ) ORDER BY col1;
3840
3941col0 |col1 |
@@ -43,56 +45,36 @@ Montreal |1704694|
4345Toronto |2731571 |
4446```
4547
46- 这些示例演示了如何在 SELECT 语句中使用 VALUES 子句:
48+ ### 在 SELECT 语句中
4749
4850``` sql
49- -- 选择单列
51+ -- 选择特定列 - 注意 VALUES 周围的括号
5052SELECT col1
5153FROM (VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 ));
5254
53- col1 |
54- -- -----+
55- 2731571 |
56- 631486 |
57- 1704694 |
58-
59- -- 选择带有别名的列
55+ -- 自定义列名 - VALUES 必须用括号括起来
6056SELECT * FROM (
6157 VALUES (' Toronto' , 2731571 ),
6258 (' Vancouver' , 631486 ),
6359 (' Montreal' , 1704694 )
6460) AS CityPopulation(City, Population);
6561
66- city |population|
67- -- -------+----------+
68- Toronto | 2731571 |
69- Vancouver| 631486 |
70- Montreal | 1704694 |
71-
72- -- 选择带有别名和排序的列
62+ -- 使用列别名和排序
7363SELECT col0 AS City, col1 AS Population
7464FROM (VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 ))
7565ORDER BY col1 DESC
7666LIMIT 1 ;
77-
78- city |population|
79- -- -----+----------+
80- Toronto| 2731571 |
8167```
8268
83- 此示例演示了如何在公共表表达式 (CTE) 中使用 VALUES 子句创建临时表:
69+ ### 使用公共表表达式 (CTE)
8470
8571``` sql
8672WITH citypopulation(city, population) AS (
8773 VALUES (' Toronto' , 2731571 ),
8874 (' Vancouver' , 631486 ),
8975 (' Montreal' , 1704694 )
9076)
91- SELECT citypopulation .city , citypopulation .population FROM citypopulation;
92-
93- city |population|
94- -- -------+----------+
95- Toronto | 2731571 |
96- Vancouver| 631486 |
97- Montreal | 1704694 |
77+ SELECT city, population FROM citypopulation;
9878```
79+
80+ > ** 重要提示** :在 FROM 子句或 CTE 中使用 VALUES 时,必须将其括在括号中:` FROM (VALUES ...) ` 或 ` AS (VALUES ...) ` 。这是必需的语法。
0 commit comments