要为空行设置默认值,请使用COALESCE()的概念。让我们首先创建一个表-create table DemoTable1863
(
FirstName varchar(20)
);
使用插入命令在表中插入一些记录-insert into DemoTable1863 values('Chris');
insert into DemoTable1863 values(NULL);
insert into DemoTable1863 values('David');
insert into DemoTable1863 values(NULL);
使用select语句显示表中的所有记录-select * from DemoTable1863;
这将产生以下输出-+-----------+
| FirstName |
+-----------+
| Chris |
| NULL |
| David |
| NULL |
+-----------+
4 rows in set (0.00 sec)
以下是为空行设置默认值的查询-select coalesce(FirstName,'UNKNOWN NAME') from DemoTable1863;
这将产生以下输出-+------------------------------------+
| coalesce(FirstName,'UNKNOWN NAME') |
+------------------------------------+
| Chris |
| UNKNOWN NAME |
| David |
| UNKNOWN NAME |
+------------------------------------+
4 rows in set (0.00 sec)