2020-03-20 18:20:59 +00:00
# 操作符 {#cao-zuo-fu}
2019-03-22 05:55:47 +00:00
所有的操作符(运算符)都会在查询时依据他们的优先级及其结合顺序在被解析时转换为对应的函数。下面按优先级从高到低列出各组运算符及其对应的函数:
2020-03-20 18:20:59 +00:00
## 下标运算符 {#xia-biao-yun-suan-fu}
2019-03-22 05:55:47 +00:00
`a[N]` – 数组中的第N个元素; 对应函数 `arrayElement(a, N)`
`a.N` – 元组中第N个元素; 对应函数 `tupleElement(a, N)`
2020-03-20 18:20:59 +00:00
## 负号 {#fu-hao}
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
`-a` – 对应函数 `negate(a)`
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
## 乘号、除号和取余 {#cheng-hao-chu-hao-he-qu-yu}
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
`a * b` – 对应函数 `multiply(a, b)`
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
`a / b` – 对应函数 `divide(a, b)`
2019-03-22 05:55:47 +00:00
`a % b` – 对应函数 `modulo(a, b)`
2020-03-20 18:20:59 +00:00
## 加号和减号 {#jia-hao-he-jian-hao}
2019-03-22 05:55:47 +00:00
`a + b` – 对应函数 `plus(a, b)`
2020-03-20 18:20:59 +00:00
`a - b` – 对应函数 `minus(a, b)`
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
## 关系运算符 {#guan-xi-yun-suan-fu}
2019-03-22 05:55:47 +00:00
`a = b` – 对应函数 `equals(a, b)`
`a == b` – 对应函数 `equals(a, b)`
`a != b` – 对应函数 `notEquals(a, b)`
`a <> b` – 对应函数 `notEquals(a, b)`
`a <= b` – 对应函数 `lessOrEquals(a, b)`
`a >= b` – 对应函数 `greaterOrEquals(a, b)`
`a < b` – 对应函数 `less(a, b)`
`a > b` – 对应函数 `greater(a, b)`
`a LIKE s` – 对应函数 `like(a, b)`
`a NOT LIKE s` – 对应函数 `notLike(a, b)`
2020-03-20 18:20:59 +00:00
`a BETWEEN b AND c` – 等价于 `a >= b AND a <= c`
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
## 集合关系运算符 {#ji-he-guan-xi-yun-suan-fu}
2019-03-22 05:55:47 +00:00
*详见此节 [IN 相关操作符 ](select.md#select-in-operators ) 。*
`a IN ...` – 对应函数 `in(a, b)`
`a NOT IN ...` – 对应函数 `notIn(a, b)`
`a GLOBAL IN ...` – 对应函数 `globalIn(a, b)`
`a GLOBAL NOT IN ...` – 对应函数 `globalNotIn(a, b)`
2020-03-20 18:20:59 +00:00
## 逻辑非 {#luo-ji-fei}
2019-03-22 05:55:47 +00:00
`NOT a` – 对应函数 `not(a)`
2020-03-20 18:20:59 +00:00
## 逻辑与 {#luo-ji-yu}
2019-03-22 05:55:47 +00:00
`a AND b` – 对应函数`and(a, b)`
2020-03-20 18:20:59 +00:00
## 逻辑或 {#luo-ji-huo}
2019-03-22 05:55:47 +00:00
`a OR b` – 对应函数 `or(a, b)`
2020-03-20 18:20:59 +00:00
## 条件运算符 {#tiao-jian-yun-suan-fu}
2019-03-22 05:55:47 +00:00
`a ? b : c` – 对应函数 `if(a, b, c)`
注意:
条件运算符会先计算表达式b和表达式c的值, 再根据表达式a的真假, 返回相应的值。如果表达式b和表达式c是 [arrayJoin() ](functions/array_join.md#functions_arrayjoin ) 函数, 则不管表达式a是真是假, 每行都会被复制展开。
2019-11-06 12:14:41 +00:00
## Operators for Working with Dates and Times {#operators-datetime}
### EXTRACT {#operator-extract}
2020-03-20 18:20:59 +00:00
``` sql
2019-11-06 12:14:41 +00:00
EXTRACT(part FROM date);
```
Extracts a part from a given date. For example, you can retrieve a month from a given date, or a second from a time.
The `part` parameter specifies which part of the date to retrieve. The following values are available:
2020-03-21 04:11:51 +00:00
- `DAY` — The day of the month. Possible values: 1– 31.
- `MONTH` — The number of a month. Possible values: 1– 12.
- `YEAR` — The year.
- `SECOND` — The second. Possible values: 0– 59.
- `MINUTE` — The minute. Possible values: 0– 59.
- `HOUR` — The hour. Possible values: 0– 23.
2019-11-06 12:14:41 +00:00
The `part` parameter is case-insensitive.
The `date` parameter specifies the date or the time to process. Either [Date ](../data_types/date.md ) or [DateTime ](../data_types/datetime.md ) type is supported.
Examples:
2020-03-20 18:20:59 +00:00
``` sql
2019-11-06 12:14:41 +00:00
SELECT EXTRACT(DAY FROM toDate('2017-06-15'));
SELECT EXTRACT(MONTH FROM toDate('2017-06-15'));
SELECT EXTRACT(YEAR FROM toDate('2017-06-15'));
```
In the following example we create a table and insert into it a value with the `DateTime` type.
2020-03-20 18:20:59 +00:00
``` sql
2019-11-06 12:14:41 +00:00
CREATE TABLE test.Orders
(
OrderId UInt64,
OrderName String,
OrderDate DateTime
)
ENGINE = Log;
```
2020-03-20 18:20:59 +00:00
``` sql
2019-11-06 12:14:41 +00:00
INSERT INTO test.Orders VALUES (1, 'Jarlsberg Cheese', toDateTime('2008-10-11 13:23:44'));
```
2020-03-20 18:20:59 +00:00
``` sql
2019-11-06 12:14:41 +00:00
SELECT
toYear(OrderDate) AS OrderYear,
toMonth(OrderDate) AS OrderMonth,
toDayOfMonth(OrderDate) AS OrderDay,
toHour(OrderDate) AS OrderHour,
toMinute(OrderDate) AS OrderMinute,
toSecond(OrderDate) AS OrderSecond
FROM test.Orders;
```
2020-03-20 18:20:59 +00:00
``` text
2019-11-06 12:14:41 +00:00
┌─OrderYear─┬─OrderMonth─┬─OrderDay─┬─OrderHour─┬─OrderMinute─┬─OrderSecond─┐
│ 2008 │ 10 │ 11 │ 13 │ 23 │ 44 │
└───────────┴────────────┴──────────┴───────────┴─────────────┴─────────────┘
```
You can see more examples in [tests ](https://github.com/ClickHouse/ClickHouse/blob/master/dbms/tests/queries/0_stateless/00619_extract.sql ).
### INTERVAL {#operator-interval}
Creates an [Interval ](../data_types/special_data_types/interval.md )-type value that should be used in arithmetical operations with [Date ](../data_types/date.md ) and [DateTime ](../data_types/datetime.md )-type values.
Example:
2020-03-20 18:20:59 +00:00
``` sql
2019-11-06 12:14:41 +00:00
SELECT now() AS current_date_time, current_date_time + INTERVAL 4 DAY + INTERVAL 3 HOUR
```
2020-03-20 18:20:59 +00:00
``` text
2019-11-06 12:14:41 +00:00
┌───current_date_time─┬─plus(plus(now(), toIntervalDay(4)), toIntervalHour(3))─┐
│ 2019-10-23 11:16:28 │ 2019-10-27 14:16:28 │
└─────────────────────┴────────────────────────────────────────────────────────┘
```
**See Also**
2020-03-21 04:11:51 +00:00
- [Interval ](../data_types/special_data_types/interval.md ) data type
- [toInterval ](functions/type_conversion_functions.md#function-tointerval ) type convertion functions
2019-11-06 12:14:41 +00:00
2020-03-20 18:20:59 +00:00
## CASE条件表达式 {#operator-case}
2019-03-22 05:55:47 +00:00
``` sql
CASE [x]
WHEN a THEN b
[WHEN ... THEN ...]
[ELSE c]
END
```
如果指定了 `x` ,该表达式会转换为 `transform(x, [a, ...], [b, ...], c)` 函数。否则转换为 `multiIf(a, b, ..., c)`
如果该表达式中没有 `ELSE c` 子句,则默认值就是 `NULL`
但 `transform` 函数不支持 `NULL` <!-- If `x` is NULL, return NULL; if `c` is NULL, work fine. -->
2020-03-20 18:20:59 +00:00
## 连接运算符 {#lian-jie-yun-suan-fu}
2019-03-22 05:55:47 +00:00
`s1 || s2` – 对应函数 `concat(s1, s2)`
2020-03-20 18:20:59 +00:00
## 创建 Lambda 函数 {#chuang-jian-lambda-han-shu}
2019-03-22 05:55:47 +00:00
`x -> expr` – 对应函数 `lambda(x, expr)`
接下来的这些操作符因为其本身是括号没有优先级:
2020-03-20 18:20:59 +00:00
## 创建数组 {#chuang-jian-shu-zu}
2019-03-22 05:55:47 +00:00
`[x1, ...]` – 对应函数 `array(x1, ...)`
2020-03-20 18:20:59 +00:00
## 创建元组 {#chuang-jian-yuan-zu}
2019-03-22 05:55:47 +00:00
`(x1, x2, ...)` – 对应函数 `tuple(x2, x2, ...)`
2020-03-20 18:20:59 +00:00
## 结合方式 {#jie-he-fang-shi}
2019-03-22 05:55:47 +00:00
所有的同级操作符从左到右结合。例如, `1 + 2 + 3` 会转换成 `plus(plus(1, 2), 3)` 。
所以,有时他们会跟我们预期的不太一样。例如, `SELECT 4 > 2 > 3` 的结果是0。
为了高效, `and` 和 `or` 函数支持任意多参数,一连串的 `AND` 和 `OR` 运算符会转换成其对应的单个函数。
2020-03-20 18:20:59 +00:00
## 判断是否为 `NULL` {#pan-duan-shi-fou-wei-null}
2019-03-22 05:55:47 +00:00
ClickHouse 支持 `IS NULL` 和 `IS NOT NULL` 。
### IS NULL {#operator-is-null}
2020-03-21 04:11:51 +00:00
- 对于 [Nullable ](../data_types/nullable.md ) 类型的值, `IS NULL` 会返回:
- `1` 值为 `NULL`
- `0` 否则
- 对于其他类型的值, `IS NULL` 总会返回 `0`
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
<!-- -->
``` bash
2019-03-22 05:55:47 +00:00
:) SELECT x+100 FROM t_null WHERE y IS NULL
SELECT x + 100
FROM t_null
WHERE isNull(y)
┌─plus(x, 100)─┐
│ 101 │
└──────────────┘
1 rows in set. Elapsed: 0.002 sec.
```
2020-03-20 18:20:59 +00:00
### IS NOT NULL {#is-not-null}
2019-03-22 05:55:47 +00:00
2020-03-21 04:11:51 +00:00
- 对于 [Nullable ](../data_types/nullable.md ) 类型的值, `IS NOT NULL` 会返回:
- `0` 值为 `NULL`
- `1` 否则
- 对于其他类型的值,`IS NOT NULL` 总会返回 `1`
2019-03-22 05:55:47 +00:00
2020-03-20 18:20:59 +00:00
<!-- -->
``` bash
2019-03-22 05:55:47 +00:00
:) SELECT * FROM t_null WHERE y IS NOT NULL
SELECT *
FROM t_null
WHERE isNotNull(y)
┌─x─┬─y─┐
│ 2 │ 3 │
└───┴───┘
1 rows in set. Elapsed: 0.002 sec.
```
2020-01-30 10:34:55 +00:00
[来源文章 ](https://clickhouse.tech/docs/en/query_language/operators/ ) <!--hide-->