NVL 函数是 SQL 中的一个内置函数,它用于在查询中处理 NULL 值
SELECT employee_id, employee_name, NVL(phone_number, 'Not available') AS phone_number
FROM employees;
SELECT employee_id, employee_name, department_id
FROM employees
WHERE NVL(department_id, -1) = -1;
SELECT department_id, AVG(NVL(salary, 0)) AS average_salary
FROM employees
GROUP BY department_id;
SELECT e.employee_id, e.employee_name, NVL(d.department_name, 'Unassigned') AS department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.department_id;
SELECT employee_id, employee_name, salary, position,
NVL(salary, 0) * CASE position
WHEN 'Manager' THEN 0.1
WHEN 'Senior Developer' THEN 0.08
ELSE 0.05
END AS bonus
FROM employees;
总之,NVL 函数在 SQL 查询中的深入应用可以帮助你更好地处理 NULL 值,从而简化查询逻辑并提高查询效率。在实际应用中,你可以根据需求灵活地使用 NVL 函数来解决各种问题。