条件函数

if(cond, then, else), cond ? operator then : else

如果cond != 0则返回then,如果cond = 0则返回else
cond必须是UInt8类型,thenelse必须存在最低的共同类型。

thenelse可以是NULL

multiIf

允许您在查询中更紧凑地编写CASE运算符。

  1. multiIf(cond_1, then_1, cond_2, then_2...else)

参数:

  • cond_N — 函数返回then_N的条件。
  • then_N — 执行时函数的结果。
  • else — 如果没有满足任何条件,则为函数的结果。

该函数接受2N + 1参数。

返回值

该函数返回值“then_N”或“else”之一,具体取决于条件cond_N

示例

存在如下一张表

  1. ┌─x─┬────y─┐
  2. 1 ᴺᵁᴸᴸ
  3. 2 3
  4. └───┴──────┘

执行查询 SELECT multiIf(isNull(y) x, y < 3, y, NULL) FROM t_null。结果:

  1. ┌─multiIf(isNull(y), x, less(y, 3), y, NULL)─┐
  2. 1
  3. ᴺᵁᴸᴸ
  4. └────────────────────────────────────────────┘

来源文章