2018-10-19 08:02:26 +00:00
2018-10-17 11:43:36 +00:00
# Tuple(T1, T2, ...)
2018-10-19 08:02:26 +00:00
元组,其中每个元素都有单独的 [类型 ](index.md#data_types )。
2018-12-12 17:28:00 +00:00
不能在表中存储元组( 除了内存表) 。它们可以用于临时列分组。在查询中, IN 表达式和带特定参数的 lambda 函数可以来对临时列进行分组。更多信息,请参阅 [IN 操作符 ](../query_language/select.md ) and [Higher order functions ](../query_language/functions/higher_order_functions.md#higher_order_functions )。
2018-10-17 11:43:36 +00:00
2018-10-19 08:02:26 +00:00
元组可以是查询的结果。在这种情况下, 对于JSON以外的文本格式, 括号中的值是逗号分隔的。在JSON格式中, 元组作为数组输出( 在方括号中) 。
2018-10-17 11:43:36 +00:00
## 创建元组
2018-10-19 08:02:26 +00:00
可以使用函数来创建元组:
2018-10-17 11:43:36 +00:00
```
tuple(T1, T2, ...)
```
创建元组的示例:
```
:) SELECT tuple(1,'a') AS x, toTypeName(x)
SELECT
(1, 'a') AS x,
toTypeName(x)
┌─x───────┬─toTypeName(tuple(1, 'a'))─┐
│ (1,'a') │ Tuple(UInt8, String) │
└─────────┴───────────────────────────┘
1 rows in set. Elapsed: 0.021 sec.
```
## 元组中的数据类型
2018-12-12 17:28:00 +00:00
在动态创建元组时, ClickHouse 会自动为元组的每一个参数赋予最小可表达的类型。如果参数为 [NULL ](../query_language/syntax.md#null-literal ),那这个元组对应元素是 [Nullable ](nullable.md )。
2018-10-17 11:43:36 +00:00
2018-10-19 08:02:26 +00:00
自动数据类型检测示例:
2018-10-17 11:43:36 +00:00
```
SELECT tuple(1, NULL) AS x, toTypeName(x)
SELECT
(1, NULL) AS x,
toTypeName(x)
┌─x────────┬─toTypeName(tuple(1, NULL))──────┐
│ (1,NULL) │ Tuple(UInt8, Nullable(Nothing)) │
└──────────┴─────────────────────────────────┘
1 rows in set. Elapsed: 0.002 sec.
```