DBAtlas · 数据库运维图谱

NULL 处理(COALESCE/NVL/NULLIF/IS NULL)

Oracle

-- COALESCE: returns the first non-NULL value (standard)
SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;

-- Oracle-specific NVL / NVL2
SELECT NVL(balance, 0) FROM customer;
SELECT NVL2(discount, 'yes', 'no') FROM orders;

-- NULLIF and IS NULL
SELECT NULLIF(a, b) FROM t;   -- NULL if a=b
SELECT name FROM customer WHERE phone IS NULL;

处理 NULL(未知值):COALESCE 返回列表中第一个非 NULL 值(标准,支持多个参数)。Oracle 的 NVL(a,b) 是两参数的等价函数;NVL2 根据值是否为 NULL 返回不同的值。NULLIF(a,b) 在两者相等时返回 NULL。判断 NULL 务必使用 IS NULL / IS NOT NULL(= NULL 不起作用)。

PostgreSQL

SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;
SELECT NULLIF(divisor, 0) FROM t;   -- prevent division by zero
SELECT name FROM customer WHERE phone IS NULL;
-- The equivalent of Oracle NVL is COALESCE

PostgreSQL 提供标准的 COALESCE、NULLIF 和 IS NULL/IS NOT NULL;请用 COALESCE 代替 Oracle 的 NVL。NULL 比较使用 IS 而不是 =;如果需要将两个 NULL 视为相等,'IS NOT DISTINCT FROM' 运算符可以做到。

MySQL

SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;

-- MySQL shortcut: IFNULL (two arguments)
SELECT IFNULL(balance, 0) FROM customer;
SELECT NULLIF(a, b) FROM t;
SELECT name FROM customer WHERE phone IS NULL;

MySQL/MariaDB 支持标准的 COALESCE 和 NULLIF;IFNULL(a,b) 是两参数的简写(类似 Oracle NVL)。<=> 运算符提供 NULL 安全的相等比较(将两个 NULL 视为相等)。NULL 使用 IS NULL / IS NOT NULL 判断。

SQL Server

SELECT name, COALESCE(phone, mobile, 'none') AS contact FROM customer;

-- SQL Server shortcut: ISNULL (two arguments)
SELECT ISNULL(balance, 0) FROM customer;
SELECT NULLIF(a, b) FROM t;
SELECT name FROM customer WHERE phone IS NULL;

SQL Server 提供标准的 COALESCE(多参数,带数据类型提升)和两参数的 ISNULL(a,b);两者存在细微差别(COALESCE 是标准的,在类型优先级上更灵活)。也支持 NULLIF 和 IS NULL。ISNULL 是 Oracle NVL 的等价物(名称相近)。

MongoDB

// COALESCE equivalent: $ifNull
db.customer.aggregate([
  { $project: { name: 1,
      contact: { $ifNull: ["$phone", "$mobile", "none"] } } }
])
// NULL / missing field check:
db.customer.find({ phone: { $in: [null] } })

在 MongoDB 聚合中,COALESCE 的等价物是 $ifNull 运算符(多参数,返回第一个不为 null/缺失的值)。在查询中,{ field: null } 既匹配值为 null 的文档,也匹配完全缺少该字段的文档;需用 { field: { $type: 'null' } } 或 $exists 明确区分。

Redis

# Instead of NULL, Redis has the concept of 'no such key/field':
EXISTS customer:1              # does the key exist?
HGET customer:1 phone        # returns nil if the field is absent
GET missing_key                  # (nil)

Redis 没有 SQL 意义上的 NULL;等价情况是键缺失或 Hash 字段缺失,此时命令返回 nil。默认值逻辑(类似 COALESCE)在应用端通过 nil 检查实现。字段是否存在通过 HEXISTS 检查。

Elasticsearch

# Default assignment when a field is missing (missing bucket / null_value)
# Find documents without the field in a query:
GET customer/_search
{ "query": { "bool": { "must_not": { "exists": { "field": "phone" } } } } }
# Map empty values to a specific value with null_value in the mapping

在 Elasticsearch 中,null 或缺失字段通过 'exists' 查询检查(must_not exists = 没有该字段的文档)。要实现类似 COALESCE 的默认值,在 mapping 中定义 'null_value',或在聚合中使用 'missing' 参数为缺失项赋值。默认值也可以通过 runtime field/脚本生成。

在 DBAtlas 中打开 →