如果column为null,MySQL查询是否返回特定的字符串?
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Name varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('John'); mysql> insert into DemoTable values(NULL); mysql> insert into DemoTable values(NULL); mysql> insert into DemoTable values('David');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+-------+ | Name | +-------+ | John | | NULL | | NULL | | David | +-------+ 4 rows in set (0.00 sec)
如果column为null,这是返回特定字符串的查询-
mysql> SELECT COALESCE(Name, 'Not Valid') from DemoTable;
输出结果
+-----------------------------+ | COALESCE(Name, 'Not Valid') | +-----------------------------+ | John | | Not Valid | | Not Valid | | David | +-----------------------------+ 4 rows in set (0.00 sec)