ใช้ COALESCE() คืนค่าตัวแทนเมื่อผลลัพธ์เป็น NULL (Empty set)
mysql> CREATE TABLE DemoTable (
-> Code VARCHAR(20)
-> );
Query OK, 0 rows affected (1.64 sec)
mysql> INSERT INTO DemoTable VALUES('10');
Query OK, 1 row affected (0.15 sec)
mysql> INSERT INTO DemoTable VALUES('45');
Query OK, 1 row affected (0.30 sec)
mysql> INSERT INTO DemoTable VALUES('78');
Query OK, 1 row affected (0.21 sec)
mysql> SELECT * FROM DemoTable;
+------+ | Code | +------+ | 10 | | 45 | | 78 | +------+ 3 rows in set (0.00 sec)
ค้นหา Code='90' (ไม่มี) แทน Empty set ด้วย 'false':
mysql> SELECT COALESCE(MAX(Code), 'false') AS WhenReturnSetIsEmpty
-> FROM DemoTable
-> WHERE Code = '90';
+----------------------+ | WhenReturnSetIsEmpty | +----------------------+ | false | +----------------------+ 1 row in set (0.00 sec)
ประโยชน์: API ได้รับ Row เสมอ (ไม่ Empty) ลด Logic ตรวจสอบ Empty Result Set ฝั่ง Application