在 MySQL 中为枚举值设置自定义信息
使用 if else 为枚举设置自定义消息。我们首先创建一个表格 −
mysql> create table DemoTable1992 ( ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY, ClientName varchar(20), isActive ENUM('Y','N') ); Query OK, 0 rows affected (0.89 sec)
使用 insert 命令在表格中插入一些记录 −
mysql> insert into DemoTable1992(ClientName,isActive) values('Chris','N'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1992(ClientName,isActive) values('Bob','N'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1992(ClientName,isActive) values('David','Y'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1992(ClientName,isActive) values('Carol','N'); Query OK, 1 row affected (0.19 sec)
使用 select 语句显示表格中的所有记录 −
mysql> select * from DemoTable1992;
这将产生以下输出 −
+----------+------------+----------+ | ClientId | ClientName | isActive | +----------+------------+----------+ | 1 | Chris | N | | 2 | Bob | N | | 3 | David | Y | | 4 | Carol | N | +----------+------------+----------+ 4 rows in set (0.00 sec)
这是使用 if else 设置自定义信息查询
mysql> select ClientName,if(isActive='Y','Active Member','Not Active Member') as Result from DemoTable1992;
这将产生以下输出 −
+------------+-------------------+ | ClientName | Result | +------------+-------------------+ | Chris | Not Active Member | | Bob | Not Active Member | | David | Active Member | | Carol | Not Active Member | +------------+-------------------+ 4 rows in set (0.00 sec)
广告