ClickHouse/docs/zh/sql-reference/functions/functions-for-nulls.md

253 lines
6.3 KiB
Markdown
Raw Normal View History

# Nullable处理函数 {#nullablechu-li-han-shu}
2019-05-05 17:38:05 +00:00
## isNull {#isnull}
2019-05-05 17:38:05 +00:00
DOCS-624: Fixing links to nowhere (#10675) * enbaskakova-DOCSUP-652 (#101) * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" Co-authored-by: elenbaskakova <elenbaskakova@yandex-team.ru> Co-authored-by: BayoNet <da-daos@yandex.ru> * Revert "enbaskakova-DOCSUP-652 (#101)" (#107) This reverts commit 639fee7610f28e421d14e535b7def3f466e7efca. * CLICKHOUSEDOCS-624: Fixed links. Was 60, became 13. * CLICKHOUSEDOCS-624: Finished fix links in Enlish version. * CLICKHOUSEDOCS-624: Fixed RU links Co-authored-by: elenaspb2019 <47083263+elenaspb2019@users.noreply.github.com> Co-authored-by: elenbaskakova <elenbaskakova@yandex-team.ru> Co-authored-by: Sergei Shtykov <bayonet@yandex-team.ru>
2020-05-06 06:13:29 +00:00
检查参数是否为[NULL](../../sql-reference/syntax.md#null-literal)。
2019-05-05 17:38:05 +00:00
isNull(x)
2019-05-05 17:38:05 +00:00
**参数**
- `x` — 一个非复合数据类型的值。
2019-05-05 17:38:05 +00:00
**返回值**
- `1` 如果`x`为`NULL`。
- `0` 如果`x`不为`NULL`。
2019-05-05 17:38:05 +00:00
**示例**
存在以下内容的表
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
2019-05-05 17:38:05 +00:00
对其进行查询
:) SELECT x FROM t_null WHERE isNull(y)
2019-05-05 17:38:05 +00:00
SELECT x
FROM t_null
WHERE isNull(y)
2019-05-05 17:38:05 +00:00
┌─x─┐
│ 1 │
└───┘
2019-05-05 17:38:05 +00:00
1 rows in set. Elapsed: 0.010 sec.
2019-05-05 17:38:05 +00:00
## isNotNull {#isnotnull}
2019-05-05 17:38:05 +00:00
DOCS-624: Fixing links to nowhere (#10675) * enbaskakova-DOCSUP-652 (#101) * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * Update docs/en/sql_reference/aggregate_functions/combinators.md Co-Authored-By: BayoNet <da-daos@yandex.ru> * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" * "docs(orNull&orDefault): Functions 'orNull&orDefault' have been edited" Co-authored-by: elenbaskakova <elenbaskakova@yandex-team.ru> Co-authored-by: BayoNet <da-daos@yandex.ru> * Revert "enbaskakova-DOCSUP-652 (#101)" (#107) This reverts commit 639fee7610f28e421d14e535b7def3f466e7efca. * CLICKHOUSEDOCS-624: Fixed links. Was 60, became 13. * CLICKHOUSEDOCS-624: Finished fix links in Enlish version. * CLICKHOUSEDOCS-624: Fixed RU links Co-authored-by: elenaspb2019 <47083263+elenaspb2019@users.noreply.github.com> Co-authored-by: elenbaskakova <elenbaskakova@yandex-team.ru> Co-authored-by: Sergei Shtykov <bayonet@yandex-team.ru>
2020-05-06 06:13:29 +00:00
检查参数是否不为 [NULL](../../sql-reference/syntax.md#null-literal).
2019-05-05 17:38:05 +00:00
isNotNull(x)
2019-05-05 17:38:05 +00:00
**参数:**
- `x` — 一个非复合数据类型的值。
2019-05-05 17:38:05 +00:00
**返回值**
- `0` 如果`x`为`NULL`。
- `1` 如果`x`不为`NULL`。
2019-05-05 17:38:05 +00:00
**示例**
存在以下内容的表
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
2019-05-05 17:38:05 +00:00
对其进行查询
:) SELECT x FROM t_null WHERE isNotNull(y)
2019-05-05 17:38:05 +00:00
SELECT x
FROM t_null
WHERE isNotNull(y)
2019-05-05 17:38:05 +00:00
┌─x─┐
│ 2 │
└───┘
2019-05-05 17:38:05 +00:00
1 rows in set. Elapsed: 0.010 sec.
2019-05-05 17:38:05 +00:00
## 合并 {#coalesce}
2019-05-05 17:38:05 +00:00
检查从左到右是否传递了«NULL»参数并返回第一个非`'NULL`参数。
2019-05-05 17:38:05 +00:00
coalesce(x,...)
2019-05-05 17:38:05 +00:00
**参数:**
- 任何数量的非复合类型的参数。所有参数必须与数据类型兼容。
2019-05-05 17:38:05 +00:00
**返回值**
- 第一个非NULL\`参数。
- `NULL`如果所有参数都是NULL\`。
2019-05-05 17:38:05 +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`。
从联系人列表中获取客户的第一个可用联系方式:
:) SELECT coalesce(mail, phone, CAST(icq,'Nullable(String)')) FROM aBook
2019-05-05 17:38:05 +00:00
SELECT coalesce(mail, phone, CAST(icq, 'Nullable(String)'))
FROM aBook
2019-05-05 17:38:05 +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
2 rows in set. Elapsed: 0.006 sec.
2019-05-05 17:38:05 +00:00
## ifNull {#ifnull}
2019-05-05 17:38:05 +00:00
如果第一个参数为«NULL»则返回第二个参数的值。
2019-05-05 17:38:05 +00:00
ifNull(x,alt)
2019-05-05 17:38:05 +00:00
**参数:**
- `x` — 要检查«NULL»的值。
- `alt` — 如果`x`为NULL\`,函数返回的值。
2019-05-05 17:38:05 +00:00
**返回值**
- 价值 `x`,如果 `x` 不是 `NULL`.
- 价值 `alt`,如果 `x``NULL`.
2019-05-05 17:38:05 +00:00
**示例**
SELECT ifNull('a', 'b')
2019-05-05 17:38:05 +00:00
┌─ifNull('a', 'b')─┐
│ a │
└──────────────────┘
2019-05-05 17:38:05 +00:00
SELECT ifNull(NULL, 'b')
2019-05-05 17:38:05 +00:00
┌─ifNull(NULL, 'b')─┐
│ b │
└───────────────────┘
2019-05-05 17:38:05 +00:00
## nullIf {#nullif}
2019-05-05 17:38:05 +00:00
如果参数相等,则返回`NULL`。
nullIf(x, y)
2019-05-05 17:38:05 +00:00
**参数:**
`x`, `y` — 用于比较的值。 它们必须是类型兼容的,否则将抛出异常。
**返回值**
- 如果参数相等,则为`NULL`。
- 如果参数不相等,则为`x`值。
2019-05-05 17:38:05 +00:00
**示例**
SELECT nullIf(1, 1)
2019-05-05 17:38:05 +00:00
┌─nullIf(1, 1)─┐
│ ᴺᵁᴸᴸ │
└──────────────┘
2019-05-05 17:38:05 +00:00
SELECT nullIf(1, 2)
2019-05-05 17:38:05 +00:00
┌─nullIf(1, 2)─┐
│ 1 │
└──────────────┘
2019-05-05 17:38:05 +00:00
## assumeNotNull {#assumenotnull}
2019-05-05 17:38:05 +00:00
将[可为空](../../sql-reference/functions/functions-for-nulls.md)类型的值转换为非`Nullable`类型的值。
2019-05-05 17:38:05 +00:00
assumeNotNull(x)
2019-05-05 17:38:05 +00:00
**参数:**
- `x` — 原始值。
2019-05-05 17:38:05 +00:00
**返回值**
- 如果`x`不为`NULL`,返回非`Nullable`类型的原始值。
- 如果`x`为`NULL`,返回对应非`Nullable`类型的默认值。
2019-05-05 17:38:05 +00:00
**示例**
存在如下`t_null`表。
SHOW CREATE TABLE t_null
2019-05-05 17:38:05 +00:00
┌─statement─────────────────────────────────────────────────────────────────┐
│ CREATE TABLE default.t_null ( x Int8, y Nullable(Int8)) ENGINE = TinyLog │
└───────────────────────────────────────────────────────────────────────────┘
2019-05-05 17:38:05 +00:00
┌─x─┬────y─┐
│ 1 │ ᴺᵁᴸᴸ │
│ 2 │ 3 │
└───┴──────┘
2019-05-05 17:38:05 +00:00
将列`y`作为`assumeNotNull`函数的参数。
SELECT assumeNotNull(y) FROM t_null
2019-05-05 17:38:05 +00:00
┌─assumeNotNull(y)─┐
│ 0 │
│ 3 │
└──────────────────┘
2019-05-05 17:38:05 +00:00
SELECT toTypeName(assumeNotNull(y)) FROM t_null
2019-05-05 17:38:05 +00:00
┌─toTypeName(assumeNotNull(y))─┐
│ Int8 │
│ Int8 │
└──────────────────────────────┘
2019-05-05 17:38:05 +00:00
## 可调整 {#tonullable}
2019-05-05 17:38:05 +00:00
将参数的类型转换为`Nullable`。
toNullable(x)
2019-05-05 17:38:05 +00:00
**参数:**
- `x` — 任何非复合类型的值。
2019-05-05 17:38:05 +00:00
**返回值**
- 输入的值,但其类型为`Nullable`。
2019-05-05 17:38:05 +00:00
**示例**
SELECT toTypeName(10)
2019-05-05 17:38:05 +00:00
┌─toTypeName(10)─┐
│ UInt8 │
└────────────────┘
2019-05-05 17:38:05 +00:00
SELECT toTypeName(toNullable(10))
2019-05-05 17:38:05 +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-->