# 9.18.条件表达式

9.18.1.案例

9.18.2.合并

9.18.3.努利夫

9.18.4.最大的最小的

本节介绍PostgreSQL中可用的符合SQL的条件表达式。

# 提示

如果您的需求超出了这些条件表达式的能力,您可能需要考虑以更具表现力的编程语言编写服务器端函数。

# 笔记

虽然合并,最大的最小的在语法上与函数类似,它们不是普通函数,因此不能与显式函数一起使用可变的数组参数。

# 9.18.1. 案例

SQL案例表达式是一个通用条件表达式,类似于其他编程语言中的if/else语句:

CASE WHEN condition THEN result
     [WHEN ...]
     [ELSE result]
END

案例只要表达式有效,就可以使用子句。每个*条件是一个返回布尔值后果如果条件的结果为真,则案例表达是最重要的后果*这符合条件,以及案例表达式未被处理。如果条件的结果不正确,则任何后续什么时候条款也是以同样的方式审查的。如果没有什么时候 *条件如果是真的,那么案例表达是最重要的后果*关于其他的条款如果其他的子句被省略,且没有条件为真,结果为空。

举个例子:

SELECT * FROM test;

 a
### Note

 As described in [Section 4.2.14](sql-expressions.html#SYNTAX-EXPRESS-EVAL), there are various situations in which subexpressions of an expression are evaluated at different times, so that the principle that “`CASE` evaluates only necessary subexpressions” is not ironclad. For example a constant `1/0` subexpression will usually result in a division-by-zero failure at planning time, even if it's within a `CASE` arm that would never be entered at run time.

### 9.18.2. `COALESCE`

[]()[]()[]()

结合(价值)[, ...])

 The `COALESCE` function returns the first of its arguments that is not null. Null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display, for example:

选择合并(描述,简短描述,“(无)”。。。

 This returns `description` if it is not null, otherwise `short_description` if it is not null, otherwise `(none)`.

 The arguments must all be convertible to a common data type, which will be the type of the result (see [Section 10.5](typeconv-union-case.html) for details).

 Like a `CASE` expression, `COALESCE` only evaluates the arguments that are needed to determine the result; that is, arguments to the right of the first non-null argument are not evaluated. This SQL-standard function provides capabilities similar to `NVL` and `IFNULL`, which are used in some other database systems.

### 9.18.3. `NULLIF`

[]()

NULLIF(值1、值2)

 The `NULLIF` function returns a null value if *`value1`* equals *`value2`*; otherwise it returns *`value1`*. This can be used to perform the inverse operation of the `COALESCE` example given above:

选择NULLIF(值,,(无)。。。

 In this example, if `value` is `(none)`, null is returned, otherwise the value of `value` is returned.

 The two arguments must be of comparable types. To be specific, they are compared exactly as if you had written `*`value1`* = *`value2`*`, so there must be a suitable `=` operator available.

 The result has the same type as the first argument — but there is a subtlety. What is actually returned is the first argument of the implied `=` operator, and in some cases that will have been promoted to match the second argument's type. For example, `NULLIF(1, 2.2)` yields `numeric`, because there is no `integer` `=` `numeric` operator, only `numeric` `=` `numeric`.

### 9.18.4. `GREATEST` and `LEAST`

[]()[]()

最大(价值)[, ...])


最小值[, ...])

 The `GREATEST` and `LEAST` functions select the largest or smallest value from a list of any number of expressions. The expressions must all be convertible to a common data type, which will be the type of the result (see [Section 10.5](typeconv-union-case.html) for details). NULL values in the list are ignored. The result will be NULL only if all the expressions evaluate to NULL.

 Note that `GREATEST` and `LEAST` are not in the SQL standard, but are a common extension. Some other databases make them return NULL if any argument is NULL, rather than only when all are NULL.