2020-03-20 18:20:59 +00:00
|
|
|
|
# Nullable处理函数 {#nullablechu-li-han-shu}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## isNull {#isnull}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
检查参数是否为[NULL](../syntax.md#null)。
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
isNull(x)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**参数**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- `x` — 一个非复合数据类型的值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- `1` 如果`x`为`NULL`。
|
|
|
|
|
- `0` 如果`x`不为`NULL`。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**示例**
|
|
|
|
|
|
|
|
|
|
存在以下内容的表
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─x─┬────y─┐
|
|
|
|
|
│ 1 │ ᴺᵁᴸᴸ │
|
|
|
|
|
│ 2 │ 3 │
|
|
|
|
|
└───┴──────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
对其进行查询
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
:) SELECT x FROM t_null WHERE isNull(y)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT x
|
|
|
|
|
FROM t_null
|
|
|
|
|
WHERE isNull(y)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─x─┐
|
|
|
|
|
│ 1 │
|
|
|
|
|
└───┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
1 rows in set. Elapsed: 0.010 sec.
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## isNotNull {#isnotnull}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
检查参数是否不为 [NULL](../syntax.md#null).
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
isNotNull(x)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**参数:**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- `x` — 一个非复合数据类型的值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- `0` 如果`x`为`NULL`。
|
|
|
|
|
- `1` 如果`x`不为`NULL`。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**示例**
|
|
|
|
|
|
|
|
|
|
存在以下内容的表
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─x─┬────y─┐
|
|
|
|
|
│ 1 │ ᴺᵁᴸᴸ │
|
|
|
|
|
│ 2 │ 3 │
|
|
|
|
|
└───┴──────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
对其进行查询
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
:) SELECT x FROM t_null WHERE isNotNull(y)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT x
|
|
|
|
|
FROM t_null
|
|
|
|
|
WHERE isNotNull(y)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─x─┐
|
|
|
|
|
│ 2 │
|
|
|
|
|
└───┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
1 rows in set. Elapsed: 0.010 sec.
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## coalesce {#coalesce}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
检查从左到右是否传递了«NULL»参数并返回第一个非`'NULL`参数。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
coalesce(x,...)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**参数:**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- 任何数量的非复合类型的参数。所有参数必须与数据类型兼容。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- 第一个非’NULL\`参数。
|
|
|
|
|
- `NULL`,如果所有参数都是’NULL\`。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**示例**
|
|
|
|
|
|
|
|
|
|
考虑可以指定多种联系客户的方式的联系人列表。
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─name─────┬─mail─┬─phone─────┬──icq─┐
|
|
|
|
|
│ client 1 │ ᴺᵁᴸᴸ │ 123-45-67 │ 123 │
|
|
|
|
|
│ client 2 │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │ ᴺᵁᴸᴸ │
|
|
|
|
|
└──────────┴──────┴───────────┴──────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
`mail`和`phone`字段是String类型,但`icq`字段是`UInt32`,所以它需要转换为`String`。
|
|
|
|
|
|
|
|
|
|
从联系人列表中获取客户的第一个可用联系方式:
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
:) SELECT coalesce(mail, phone, CAST(icq,'Nullable(String)')) FROM aBook
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT coalesce(mail, phone, CAST(icq, 'Nullable(String)'))
|
|
|
|
|
FROM aBook
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─name─────┬─coalesce(mail, phone, CAST(icq, 'Nullable(String)'))─┐
|
|
|
|
|
│ client 1 │ 123-45-67 │
|
|
|
|
|
│ client 2 │ ᴺᵁᴸᴸ │
|
|
|
|
|
└──────────┴──────────────────────────────────────────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
2 rows in set. Elapsed: 0.006 sec.
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## ifNull {#ifnull}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
如果第一个参数为«NULL»,则返回第二个参数的值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
ifNull(x,alt)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**参数:**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- `x` — 要检查«NULL»的值。
|
|
|
|
|
- `alt` — 如果`x`为’NULL\`,函数返回的值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- The value `x`, if `x` is not `NULL`.
|
|
|
|
|
- The value `alt`, if `x` is `NULL`.
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**示例**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT ifNull('a', 'b')
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─ifNull('a', 'b')─┐
|
|
|
|
|
│ a │
|
|
|
|
|
└──────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT ifNull(NULL, 'b')
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─ifNull(NULL, 'b')─┐
|
|
|
|
|
│ b │
|
|
|
|
|
└───────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## nullIf {#nullif}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
如果参数相等,则返回`NULL`。
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
nullIf(x, y)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**参数:**
|
|
|
|
|
|
|
|
|
|
`x`, `y` — 用于比较的值。 它们必须是类型兼容的,否则将抛出异常。
|
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- 如果参数相等,则为`NULL`。
|
|
|
|
|
- 如果参数不相等,则为`x`值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**示例**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT nullIf(1, 1)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─nullIf(1, 1)─┐
|
|
|
|
|
│ ᴺᵁᴸᴸ │
|
|
|
|
|
└──────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT nullIf(1, 2)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─nullIf(1, 2)─┐
|
|
|
|
|
│ 1 │
|
|
|
|
|
└──────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## assumeNotNull {#assumenotnull}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
将[Nullable](../../data_types/nullable.md)类型的值转换为非`Nullable`类型的值。
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
assumeNotNull(x)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**参数:**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- `x` — 原始值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- 如果`x`不为`NULL`,返回非`Nullable`类型的原始值。
|
|
|
|
|
- 如果`x`为`NULL`,返回对应非`Nullable`类型的默认值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**示例**
|
|
|
|
|
|
|
|
|
|
存在如下`t_null`表。
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SHOW CREATE TABLE t_null
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─statement─────────────────────────────────────────────────────────────────┐
|
|
|
|
|
│ CREATE TABLE default.t_null ( x Int8, y Nullable(Int8)) ENGINE = TinyLog │
|
|
|
|
|
└───────────────────────────────────────────────────────────────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─x─┬────y─┐
|
|
|
|
|
│ 1 │ ᴺᵁᴸᴸ │
|
|
|
|
|
│ 2 │ 3 │
|
|
|
|
|
└───┴──────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
将列`y`作为`assumeNotNull`函数的参数。
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT assumeNotNull(y) FROM t_null
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─assumeNotNull(y)─┐
|
|
|
|
|
│ 0 │
|
|
|
|
|
│ 3 │
|
|
|
|
|
└──────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT toTypeName(assumeNotNull(y)) FROM t_null
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─toTypeName(assumeNotNull(y))─┐
|
|
|
|
|
│ Int8 │
|
|
|
|
|
│ Int8 │
|
|
|
|
|
└──────────────────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-20 18:20:59 +00:00
|
|
|
|
## toNullable {#tonullable}
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
将参数的类型转换为`Nullable`。
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
toNullable(x)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**参数:**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- `x` — 任何非复合类型的值。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**返回值**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
- 输入的值,但其类型为`Nullable`。
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
|
|
|
|
**示例**
|
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT toTypeName(10)
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─toTypeName(10)─┐
|
|
|
|
|
│ UInt8 │
|
|
|
|
|
└────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
SELECT toTypeName(toNullable(10))
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-03-21 04:11:51 +00:00
|
|
|
|
┌─toTypeName(toNullable(10))─┐
|
|
|
|
|
│ Nullable(UInt8) │
|
|
|
|
|
└────────────────────────────┘
|
2019-05-05 17:38:05 +00:00
|
|
|
|
|
2020-01-30 10:34:55 +00:00
|
|
|
|
[来源文章](https://clickhouse.tech/docs/en/query_language/functions/functions_for_nulls/) <!--hide-->
|