NULL 相关函数对比
| 需求 | 推荐写法 |
|---|---|
| 把 NULL 换成默认值 | coalesce(col, '默认') |
| 防止除零 | a / nullif(b, 0) |
| 判断是否为空 | where col is null |
| 排序时 NULL 放最后 | order by col nulls last(Oracle/PG) order by (col is null), col(MySQL) |
| 取第一个非空值 | coalesce(a, b, c, 'none') |
| 需求 | 推荐写法 |
|---|---|
| 把 NULL 换成默认值 | coalesce(col, '默认') |
| 防止除零 | a / nullif(b, 0) |
| 判断是否为空 | where col is null |
| 排序时 NULL 放最后 | order by col nulls last(Oracle/PG) order by (col is null), col(MySQL) |
| 取第一个非空值 | coalesce(a, b, c, 'none') |