ใช้ IS NULL เพื่อทดสอบค่า NULL ครับ
สร้างตารางตัวอย่าง
mysql> create table DemoTable (
-> Number1 int,
-> Number2 int
-> );
Query OK, 0 rows affected (0.62 sec)
เพิ่มข้อมูลตัวอย่าง
mysql> insert into DemoTable values(1, NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(NULL, NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3, NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL, 90); Query OK, 1 row affected (0.11 sec)
แสดงข้อมูลทั้งหมด
mysql> select * from DemoTable;
ผลลัพธ์
+---------+---------+ | Number1 | Number2 | +---------+---------+ | 1 | NULL | | NULL | NULL | | 3 | NULL | | NULL | 90 | +---------+---------+ 4 rows in set (0.00 sec)
คำสั่งนับ NULL ในสองคอลัมน์พร้อมกัน
mysql> select
-> (select count(*) from DemoTable where Number1 is null) as FirstColumnNullValue,
-> (select count(*) from DemoTable where Number2 is null) as SecondColumnNullValue
-> ;
ผลลัพธ์
+----------------------+-----------------------+ | FirstColumnNullValue | SecondColumnNullValue | +----------------------+-----------------------+ | 2 | 3 | +----------------------+-----------------------+ 1 row in set (0.00 sec)
คำอธิบาย: ใช้ subquery นับ NULL แต่ละคอลัมน์ แล้วรวมแสดงในแถวเดียว ใช้ alias ตั้งชื่อคอลัมน์ผลลัพธ์