The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.

Condition expression

Last updated: 2024-08-22 16:23:50

CASE

The database supports CASE expressions, similar to the IF/ELSE feature in other languages.
Example:
SELECT a,
CASE WHEN a=1 THEN 'one'
WHEN a=2 THEN 'two'
ELSE 'other'
END
FROM test;

COALESCE

COALESCE returns the first non-NULL value among its parameters. If all parameters are NULL, it returns NULL.
SELECT COALESCE(null,1,2,null) ;

coalesce

----------

1

NULLIF

NULLIF(value1, value2) returns: if value1 and value2 are equal, it returns NULL. Otherwise, it returns value1.

GREATEST and LEAST

These two functions return the largest or smallest value from a list of values, respectively. When all parameters are NULL, it returns NULL.