用于显示空列的自定义文本的 MySQL 查询
我们首先创建一个表 -
mysql> create table DemoTable -> ( -> FirstName varchar(20) -> ); Query OK, 0 rows affected (0.77 sec)
使用 insert 命令向表中插入一些记录 -
mysql> insert into DemoTable values('Chris'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(''); Query OK, 1 row affected (0.13 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将生成以下输出 -
+-----------+ | FirstName | +-----------+ | Chris | | | | David | | Bob | | | +-----------+ 5 rows in set (0.00 sec)
以下是显示空列的自定义文本的查询 -
mysql> select if(FirstName='','Not Applicable',FirstName) as Status from DemoTable;
这将生成以下输出 -
+----------------+ | Status | +----------------+ | Chris | | Not Applicable | | David | | Bob | | Not Applicable | +----------------+ 5 rows in set (0.00 sec)
广告