MySQL查询在获取数据时用几列中的空字符串替换空值
为此,可以使用IFNULL()或COALESCE()。让我们首先创建一个表-
create table DemoTable1849 ( ClientFirstName varchar(20), ClientLastName varchar(20) );
使用插入命令在表中插入一些记录-
insert into DemoTable1849 values('John',NULL); insert into DemoTable1849 values(NULL,'Miller'); insert into DemoTable1849 values(NULL,NULL); insert into DemoTable1849 values('Chris','Brown');
使用select语句显示表中的所有记录-
select * from DemoTable1849;
这将产生以下输出-
+-----------------+----------------+ | ClientFirstName | ClientLastName | +-----------------+----------------+ | John | NULL | | NULL | Miller | | NULL | NULL | | Chris | Brown | +-----------------+----------------+ 4 rows in set (0.00 sec)
这是在获取数据时用几列中的空字符串替换空值的查询-
select ifnull(ClientFirstName,'') as ClientFirstName,ifnull(ClientLastName,'') as ClientLastName from DemoTable1849;
这将产生以下输出-
+-----------------+----------------+ | ClientFirstName | ClientLastName | +-----------------+----------------+ | John | | | | Miller | | | | | Chris | Brown | +-----------------+----------------+ 4 rows in set (0.00 sec)