ใช้ฟังก์ชัน IF() ตรวจสอบค่าว่าง ('') แล้วแทนที่ด้วยค่าที่ต้องการ
สร้างตารางและเพิ่มข้อมูล (มีค่าว่าง)
mysql> create table DemoTable837(Name varchar(100));
Query OK, 0 rows affected (0.53 sec)
mysql> insert into DemoTable837 values('Chris'); Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable837 values(''); Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable837 values('Robert'); Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable837 values(''); Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable837 values('David'); Query OK, 1 row affected (1.13 sec)
แสดงข้อมูลก่อนแก้ไข
mysql> select * from DemoTable837;
+--------+ | Name | +--------+ | Chris | | | | Robert | | | | David | +--------+ 5 rows in set (0.00 sec)
คิวรีแทนที่ค่าว่างด้วย 'Adam'
mysql> select if(Name='', 'Adam', Name) from DemoTable837;
ผลลัพธ์:
+--------------------------+ | if(Name='', 'Adam',Name) | +--------------------------+ | Chris | | Adam | | Robert | | Adam | | David | +--------------------------+ 5 rows in set (0.00 sec)
หมายเหตุ: คิวรีนี้แสดงผลเฉพาะ ไม่ได้อัปเดตข้อมูลในตารางจริง หากต้องการแก้ไขจริง ให้ใช้ UPDATE แทน