Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

MySQL: ใช้ IFNULL ร่วมกับ ORDER BY สำหรับจัดการค่า NULL

ฟังก์ชัน IFNULL() ใน MySQL สามารถใช้ร่วมกับ ORDER BY เพื่อจัดการค่า NULL ให้เรียงลำดับตามต้องการ โดยแทนค่า NULL ด้วยค่าสำรอง (fallback value)

รูปแบบคำสั่ง

SELECT * FROM table_name
ORDER BY IFNULL(column1, column2);
  • หาก column1 ไม่ใช่ NULL → ใช้ column1 เรียง
  • หาก column1 เป็น NULL → ใช้ column2 เรียงแทน

ตัวอย่างการใช้งาน

1. สร้างตารางสินค้าที่มีราคาส่งและราคาปลีก (บางรายการ NULL)

CREATE TABLE IfNullDemo (
    Id INT NOT NULL AUTO_INCREMENT,
    ProductName VARCHAR(10),
    ProductWholePrice FLOAT,
    ProductRetailPrice FLOAT,
    PRIMARY KEY (Id)
);

INSERT INTO IfNullDemo (ProductName, ProductWholePrice, ProductRetailPrice) VALUES
('Product-1', 99.50, 150.50),
('Product-2', NULL, 76.56),
('Product-3', 105.40, NULL),
('Product-4', NULL, NULL),
('Product-5', 209.90, 400.50);

ข้อมูลในตาราง:

+----+-------------+-------------------+--------------------+
| Id | ProductName | ProductWholePrice | ProductRetailPrice |
+----+-------------+-------------------+--------------------+
|  1 | Product-1   |            99.50  |            150.50  |
|  2 | Product-2   |            NULL   |             76.56  |
|  3 | Product-3   |           105.40  |            NULL    |
|  4 | Product-4   |            NULL   |            NULL    |
|  5 | Product-5   |           209.90  |            400.50  |
+----+-------------+-------------------+--------------------+

2. เรียงลำดับ: ใช้ WholePrice ถ้ามี ไม่งั้นใช้ RetailPrice

SELECT * FROM IfNullDemo
ORDER BY IFNULL(ProductWholePrice, ProductRetailPrice);

ผลลัพธ์ (น้อยไปมาก):

+----+-------------+-------------------+--------------------+
| Id | ProductName | ProductWholePrice | ProductRetailPrice |
+----+-------------+-------------------+--------------------+
|  4 | Product-4   | NULL              | NULL               |  -- ทั้งคู่ NULL → เรียงก่อนสุด
|  2 | Product-2   | NULL              | 76.56              |  -- ใช้ RetailPrice = 76.56
|  1 | Product-1   | 99.50             | 150.50             |  -- ใช้ WholePrice = 99.50
|  3 | Product-3   | 105.40            | NULL               |  -- ใช้ WholePrice = 105.40
|  5 | Product-5   | 209.90            | 400.50             |  -- ใช้ WholePrice = 209.90
+----+-------------+-------------------+--------------------+

ฟังก์ชันที่เกี่ยวข้อง

ฟังก์ชันคำอธิบายตัวอย่าง
IFNULL(expr1, expr2)คืน expr1 ถ้าไม่ NULL, ไม่งั้น expr2IFNULL(NULL, 0) = 0
COALESCE(expr1, expr2, ...)คืนค่าแรกที่ไม่ NULL (มาตรฐาน SQL)COALESCE(NULL, NULL, 3) = 3
NULLIF(expr1, expr2)คืน NULL ถ้า expr1 = expr2NULLIF(5,5) = NULL

การใช้ COALESCE แทน IFNULL (แนะนำ)

SELECT * FROM IfNullDemo
ORDER BY COALESCE(ProductWholePrice, ProductRetailPrice, 0);

COALESCE รองรับหลายค่า และเป็นมาตรฐาน ANSI SQL

จัดการ NULL ให้อยู่ท้าย (NULLS LAST)

MySQL เริ่มต้น เรียง NULL ก่อน (NULLS FIRST) หากต้องการ NULL ท้าย:

ORDER BY IFNULL(ProductWholePrice, ProductRetailPrice) IS NULL,  -- NULL ก่อน (0) / ไม่ NULL หลัง (1)
         IFNULL(ProductWholePrice, ProductRetailPrice);

หรือ MySQL 8.0+ ใช้:

ORDER BY IFNULL(ProductWholePrice, ProductRetailPrice) NULLS LAST;

(MySQL 8.0.2+ รองรับ NULLS FIRST/LAST syntax)