SQL NULLIF 函式

摘要:在本教程中,您將學習如何使用 SQL NULLIF 函式來比較兩個值,並在它們相等時返回 NULL

SQL NULLIF 函式簡介 #

NULLIF 函式比較兩個值,如果它們相等,則返回 NULL

這是 NULLIF 函式的語法:

NULLIF(value1,value2);Code language: SQL (Structured Query Language) (sql)

如果 value1 等於 value2NULLIF 函式返回 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)

試一試

其次,articles 表中插入一些行

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_idtitleexcerpt

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

測驗 #

資料庫 #

本教程是否有幫助?
© .