1
1
---
2
2
title : VALUES
3
3
---
4
-
5
4
import FunctionDescription from '@site/src /components/FunctionDescription';
6
5
7
6
<FunctionDescription description =" Introduced or updated: v1.2.65 " />
8
7
9
- VALUES 子句用于显式定义一组行,以便在查询中使用。它允许您提供一个值列表,这些值可以用作 SQL 语句中的临时表 。
8
+ VALUES 子句通过显式定义数据行来创建内联表。这个临时表可以直接使用,也可以在其他 SQL 语句中使用 。
10
9
11
10
## 语法
12
11
13
12
``` 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
+ [ ... ]
15
16
```
16
17
17
- - VALUES 子句后跟用括号括起来的值集。
18
- - 每个值集表示要插入到临时表中的一行。
19
- - 在每个值集中,各个值以逗号分隔,并对应于临时表的列。
20
- - 当您插入多行而不指定列名时,Databend 会自动分配默认列名,如 _ col0_ 、_ col1_ 、_ col2_ 等。
18
+ ** 要点:**
19
+ - 当 VALUES 子句在 FROM 子句中使用时,必须用括号括起来:` FROM (VALUES ...) `
20
+ - 每个带括号的表达式组代表一行
21
+ - 列名自动分配为 ** col0** 、** col1** 等(从零开始的索引)
22
+ - 您可以使用表别名提供自定义列名
21
23
22
24
## 示例
23
25
24
- 这些示例演示了如何使用 VALUES 子句以各种格式显示城市数据:直接显示或按人口排序:
26
+ ### 基本用法
25
27
26
28
``` sql
27
- -- 直接返回数据
29
+ -- 直接使用,自动生成列名 (col0, col1)
28
30
VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 );
29
31
30
32
col0 |col1 |
@@ -33,7 +35,7 @@ Toronto |2731571|
33
35
Vancouver| 631486 |
34
36
Montreal |1704694 |
35
37
36
- -- 排序数据
38
+ -- 使用 ORDER BY
37
39
VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 ) ORDER BY col1;
38
40
39
41
col0 |col1 |
@@ -43,56 +45,36 @@ Montreal |1704694|
43
45
Toronto |2731571 |
44
46
```
45
47
46
- 这些示例演示了如何在 SELECT 语句中使用 VALUES 子句:
48
+ ### 在 SELECT 语句中
47
49
48
50
``` sql
49
- -- 选择单列
51
+ -- 选择特定列 - 注意 VALUES 周围的括号
50
52
SELECT col1
51
53
FROM (VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 ));
52
54
53
- col1 |
54
- -- -----+
55
- 2731571 |
56
- 631486 |
57
- 1704694 |
58
-
59
- -- 选择带有别名的列
55
+ -- 自定义列名 - VALUES 必须用括号括起来
60
56
SELECT * FROM (
61
57
VALUES (' Toronto' , 2731571 ),
62
58
(' Vancouver' , 631486 ),
63
59
(' Montreal' , 1704694 )
64
60
) AS CityPopulation(City, Population);
65
61
66
- city |population|
67
- -- -------+----------+
68
- Toronto | 2731571 |
69
- Vancouver| 631486 |
70
- Montreal | 1704694 |
71
-
72
- -- 选择带有别名和排序的列
62
+ -- 使用列别名和排序
73
63
SELECT col0 AS City, col1 AS Population
74
64
FROM (VALUES (' Toronto' , 2731571 ), (' Vancouver' , 631486 ), (' Montreal' , 1704694 ))
75
65
ORDER BY col1 DESC
76
66
LIMIT 1 ;
77
-
78
- city |population|
79
- -- -----+----------+
80
- Toronto| 2731571 |
81
67
```
82
68
83
- 此示例演示了如何在公共表表达式 (CTE) 中使用 VALUES 子句创建临时表:
69
+ ### 使用公共表表达式 (CTE)
84
70
85
71
``` sql
86
72
WITH citypopulation(city, population) AS (
87
73
VALUES (' Toronto' , 2731571 ),
88
74
(' Vancouver' , 631486 ),
89
75
(' Montreal' , 1704694 )
90
76
)
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;
98
78
```
79
+
80
+ > ** 重要提示** :在 FROM 子句或 CTE 中使用 VALUES 时,必须将其括在括号中:` FROM (VALUES ...) ` 或 ` AS (VALUES ...) ` 。这是必需的语法。
0 commit comments