摘要:在本教程中,您将学习如何使用 SQL NULLIF 函数来比较两个值,并在它们相等时返回 NULL。
SQL NULLIF 函数简介 #
NULLIF 函数比较两个值,如果它们相等,则返回 NULL。
这是 NULLIF 函数的语法:
NULLIF(value1,value2);Code language: SQL (Structured Query Language) (sql)如果 value1 等于 value2,NULLIF 函数返回 NULL。如果两个值不相等,则返回 value1。
NULLIF 函数等效于以下搜索 CASE 表达式:
CASE
WHEN value1 = value1 THEN NULL
ELSE value1
ENDCode language: SQL (Structured Query Language) (sql)基本的 SQL NULLIF 函数示例 #
以下示例使用 NULLIF 函数,两个参数都是 100:
SELECT
NULLIF(100, 100) result;Code language: SQL (Structured Query Language) (sql)输出
result
--------
NULLCode language: SQL (Structured Query Language) (sql)以下示例返回第一个参数 100,因为 100 不等于 200:
SELECT
NULLIF(100, 200) result;Code language: SQL (Structured Query Language) (sql) result
--------
100Code language: SQL (Structured Query Language) (sql)以下语句返回第一个参数 100,因为 100 不等于 NULL。
SELECT
NULLIF(100, NULL) result;Code language: SQL (Structured Query Language) (sql)输出
result
--------
100Code language: SQL (Structured Query Language) (sql)以下语句返回第一个参数 NULL,因为 NULL 不等于 100:
SELECT
NULLIF(NULL, 100) result;Code language: SQL (Structured Query Language) (sql)输出
result
--------
NULLCode language: SQL (Structured Query Language) (sql)以下语句返回 NULL,因为字符串 SQL 等于字符串 SQL。
SELECT
NULLIF('SQL', 'SQL') result;Code language: SQL (Structured Query Language) (sql)输出
result
--------
NULLCode language: SQL (Structured Query Language) (sql)对表数据使用 NULLIF 函数 #
首先,创建一个名为 articles 的新表来存储文章:
CREATE TABLE articles (
article_id INT PRIMARY KEY,
title VARCHAR(255) NOT NULL,
excerpt VARCHAR(255),
body TEXT
);Code language: SQL (Structured Query Language) (sql)INSERT INTO
articles (article_id, title, excerpt, body)
VALUES
(
1,
'SQL NULLIF function',
'',
'This tutorial shows you how to use the SQL NULLIF function'
),
(
2,
'SQL tutorial',
'Learn how to use SQL at sqltutorial.org',
'You will learn SQL with practical examples'
),
(
3,
'SQL query',
NULL,
'You will learn how to use SELECT statement to query data from tables'
);Code language: SQL (Structured Query Language) (sql)第三,从 articles 表中检索 article_id、title 和 excerpt:
SELECT
article_id,
title,
excerpt
FROM
articles;Code language: SQL (Structured Query Language) (sql)输出
article_id | title | excerpt
------------+---------------------+-----------------------------------------
1 | SQL NULLIF function |
2 | SQL tutorial | Learn how to use SQL at sqltutorial.org
3 | SQL query | NULLCode language: SQL (Structured Query Language) (sql)如果 excerpt(摘要)不可用,我们可以使用 body(正文)的前 50 个字符来代替。
为此,我们使用 COALESCE 函数,如果 excerpt 列不为 NULL,则返回该列,否则返回 body 的前 50 个字符。
SELECT
article_id,
title,
COALESCE(excerpt, LEFT(body, 50)) AS summary
FROM
articles;Code language: SQL (Structured Query Language) (sql)输出
article_id | title | summary
------------+---------------------+----------------------------------------------------
1 | SQL NULLIF function |
2 | SQL tutorial | Learn how to use SQL at sqltutorial.org
3 | SQL query | You will learn how to use SELECT statement to querCode language: SQL (Structured Query Language) (sql)然而,文章 ID 为 1 的 summary(总结)列是空的。要解决这个问题,您可能已经猜到,我们使用 NULLIF 函数。
SELECT
article_id,
title,
COALESCE(NULLIF(excerpt,''), LEFT(body, 50)) AS summary
FROM
articles;Code language: SQL (Structured Query Language) (sql)输出
article_id | title | summary
------------+---------------------+----------------------------------------------------
1 | SQL NULLIF function | This tutorial shows you how to use the SQL NULLIF
2 | SQL tutorial | Learn how to use SQL at sqltutorial.org
3 | SQL query | You will learn how to use SELECT statement to querCode language: SQL (Structured Query Language) (sql)如果 excerpt 是空的,NULLIF 函数返回 NULL,否则它返回摘要。然后,剩下的部分由 COALESCE 函数处理。
摘要 #
- 使用
NULLIF函数来比较两个值,并在它们相等时返回NULL。
测验 #
数据库 #
本教程是否有帮助?