COALESCE 函数是 SQL 中处理 NULL 值的一个非常强大的工具。它可以接受一系列的参数,并返回第一个非 NULL 的值。如果所有的参数都是 NULL,则返回 NULL。
基本语法
COALESCE(value1, value2, ..., value_n)
COALESCE 会从左到右依次检查参数,并返回第一个非 NULL 的值。
示例数据库和表结构
假设我们有一个名为 employees 的表,用于存储员工信息,其中有部分列可能包含 NULL 值。
-- 创建数据库
CREATE DATABASE company;
-- 选择数据库
USE company;
-- 创建表 employees
CREATE TABLE employees (
emp_id INT AUTO_INCREMENT PRIMARY KEY,
emp_name VARCHAR(100) NOT NULL,
emp_position VARCHAR(100),
emp_salary DECIMAL(10, 2),
bonus DECIMAL(10, 2),
hire_date DATE
);
-- 插入示例数据到 employees 表
INSERT INTO employees (emp_name, emp_position, emp_salary, bonus, hire_date)
VALUES
('John Doe', 'Manager', 75000.00, NULL, '2023-10-01'),
('Jane Smith', 'Developer', NULL, 5000.00, '2023-09-01'),
('Alice Johnson', 'Analyst', 50000.00, NULL, '2023-08-01'),
('Bob Brown', 'Developer', NULL, NULL, '2023-07-01');
1. 使用 COALESCE 在 SELECT 语句中
假设我们希望从 employees 表中查询每位员工的薪资信息,如果 emp_salary 为 NULL,则使用 bonus,如果 bonus 也为 NULL,则返回默认值 0。
SELECT emp_id,
emp_name,
COALESCE(emp_salary, bonus, 0) AS effective_salary
FROM employees;
结果:
emp_id | emp_name | effective_salary
-------|---------------|------------------
1 | John Doe | 0.00
2 | Jane Smith | 5000.00
3 | Alice Johnson | 50000.00
4 | Bob Brown | 0.00
2. 使用 COALESCE 在 UPDATE 语句中
假设我们希望更新员工的 emp_salary 列,如果它为 NULL,则将其设置为 bonus 的值,如果 bonus 也为 NULL,则将其设置为默认值 30000。
UPDATE employees
SET emp_salary = COALESCE(emp_salary, bonus, 30000.00)
WHERE emp_salary IS NULL;
更新后的表:
emp_id | emp_name | emp_position | emp_salary | bonus | hire_date
-------|---------------|--------------|------------|-------|-----------
1 | John Doe | Manager | 30000.00 | NULL | 2023-10-01
2 | Jane Smith | Developer | 5000.00 | 5000 | 2023-09-01
3 | Alice Johnson | Analyst | 50000.00 | NULL | 2023-08-01
4 | Bob Brown | Developer | 30000.00 | NULL | 2023-07-01
3. 使用 COALESCE 在 INSERT 语句中
在插入数据时,可以使用 COALESCE 为 NULL 值提供默认值。例如,我们要插入一条新记录,如果 emp_salary 为 NULL,则使用 bonus 的值,如果 bonus 也为 NULL,则设置为默认值 35000。
INSERT INTO employees (emp_name, emp_position, emp_salary, bonus, hire_date)
VALUES ('Charlie White', 'Developer', COALESCE(NULL, 2000.00, 35000.00), 2000.00, '2023-11-01');
插入后的数据:
emp_id | emp_name | emp_position | emp_salary | bonus | hire_date
-------|---------------|--------------|------------|-------|-----------
1 | John Doe | Manager | 30000.00 | NULL | 2023-10-01
2 | Jane Smith | Developer | 5000.00 | 5000 | 2023-09-01
3 | Alice Johnson | Analyst | 50000.00 | NULL | 2023-08-01
4 | Bob Brown | Developer | 30000.00 | NULL | 2023-07-01
5 | Charlie White | Developer | 2000.00 | 2000 | 2023-11-01
4. 使用 COALESCE 在 DELETE 语句中
虽然 COALESCE 语句在删除操作中不常用,但在某些复杂条件下可以使用。
假设我们希望删除所有 emp_salary 和 bonus 都为 NULL 的记录:
DELETE FROM employees
WHERE COALESCE(emp_salary, bonus) IS NULL;
删除后的表:
emp_id | emp_name | emp_position | emp_salary | bonus | hire_date
-------|---------------|--------------|------------|-------|-----------
2 | Jane Smith | Developer | 5000.00 | 5000 | 2023-09-01
3 | Alice Johnson | Analyst | 50000.00 | NULL | 2023-08-01
5 | Charlie White | Developer | 2000.00 | 2000 | 2023-11-01
5. 使用 COALESCE 在 JOIN 语句中
假设我们有另一个表 departments,用于存储部门信息:
CREATE TABLE departments (
dept_id INT AUTO_INCREMENT PRIMARY KEY,
dept_name VARCHAR(100),
manager_id INT
);
-- 插入示例数据
INSERT INTO departments (dept_name, manager_id)
VALUES
('Engineering', 1),
('Research', NULL),
('Marketing', 2);
现在我们希望查询每个部门的名称及其经理的姓名,如果 manager_id 为 NULL,则显示 "No Manager":
SELECT d.dept_name,
COALESCE(e.emp_name, 'No Manager') AS manager_name
FROM departments d
LEFT JOIN employees e ON d.manager_id = e.emp_id;
结果:
dept_name | manager_name
------------|--------------
Engineering | John Doe
Research | No Manager
Marketing | Jane Smith
小结
COALESCE 函数在处理 NULL 值时非常有用,它可以在一系列值中返回第一个非 NULL 的值。通过使用 COALESCE,可以确保查询结果中不会出现 NULL 值,从而提高数据的完整性和可读性。上述示例展示了如何在 SELECT、UPDATE、INSERT 和 DELETE 语句中使用 COALESCE 函数,以解决各种 NULL 值处理问题。