用具体值替换 MySQL 表中空白的值
我们首先创建一个表格 −
mysql> create table DemoTable837(Name varchar(100)); Query OK, 0 rows affected (0.53 sec)
使用 insert 命令在表中插入一些记录 −
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)
利用 select 语句在表中显示所有记录 −
mysql> select *from DemoTable837;
这将产生以下输出 −
+--------+ | Name | +--------+ | Chris | | | | Robert | | | | David | +--------+ 5 rows in set (0.00 sec)
以下是使用特定值替换空值的相关查询 −
mysql> select if(Name='', 'Adam',Name) from DemoTable837;
这将产生以下输出 −
+--------------------------+ | if(Name='', 'Adam',Name) | +--------------------------+ | Chris | | Adam | | Robert | | Adam | | David | +--------------------------+ 5 rows in set (0.00 sec)
广告