MySQL CASE语句将自定义值放置为NULL
让我们首先创建一个表-
mysql> create table DemoTable(
FirstName varchar(20)
);
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('John');mysql> insert into DemoTable values(NULL);
mysql> insert into DemoTable values('Adam');
mysql> insert into DemoTable values(NULL);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+| FirstName |
+-----------+
| John |
| NULL |
| Adam |
| NULL |
+-----------+
4 rows in set (0.00 sec)
现在让我们使用CASE语句-
mysql> select case when FirstName is NULL then 'UNKNOWN NAME' else FirstName end AS FirstName from DemoTable;
这将产生以下输出-
+--------------+| FirstName |
+--------------+
| John |
| UNKNOWN NAME |
| Adam |
| UNKNOWN NAME |
+--------------+
4 rows in set (0.00 sec)
我们还可以使用IFNULL()
-获得相同的结果
mysql> select IFNULL(FirstName,'UNKNOWN NAME') AS FirstName from DemoTable;
这将产生以下输出-
+--------------+| FirstName |
+--------------+
| John |
| UNKNOWN NAME |
| Adam |
| UNKNOWN NAME |
+--------------+
4 rows in set (0.00 sec)
以上是 MySQL CASE语句将自定义值放置为NULL 的全部内容, 来源链接: utcz.com/z/321895.html