返回文章列表

Article

MySQL COALESCE 函数

介绍 COALESCE(value,...) Returns the first non- NULL value in the list, or NULL if there are no non- NULL values. 官方文档介绍的很清楚,该函数返回参数列表中第一个非NULL的值,如果没有非NULL的值则返回NU

更新于

介绍

COALESCE(value,...)

Returns the first non-NULL value in the list, or NULL if there are no non-NULL values.

官方文档介绍的很清楚,该函数返回参数列表中第一个非NULL的值,如果没有非NULL的值则返回NULL。

1
2
3
4
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL

应用实例

表中存在字段create_timeupdate_time,现在需要按更新时间倒序排序,由于更新时间可能为NULL,若为NULL时按创建时间排。

1
ORDER BY COALESCE(update_time, create_time) DESC