MySQL IF() 用于展示自定义的 YES 或 NO 消息


先创建一个表 -

mysql> create table DemoTable1850
     (
     OrderStatus varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

使用插入命令向表中插入一些记录 -

mysql> insert into DemoTable1850 values('Yes');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1850 values('No');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1850 values('Yes');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1850 values('Yes');
Query OK, 1 row affected (0.00 sec)

使用选择语句从表中展示所有记录 -

mysql> select * from DemoTable1850;

这将产生以下输出 -

+-------------+
| OrderStatus |
+-------------+
| Yes         |
| No          |
| Yes         |
| Yes         |
+-------------+
4 rows in set (0.00 sec)

以下是使用 IF() 展示自定义消息的查询

mysql> select if(OrderStatus='Yes','Order has been placed','Failed') as Status from DemoTable1850;

这将产生以下输出 -

+-----------------------+
| Status                |
+-----------------------+
| Order has been placed |
| Failed                |
| Order has been placed |
| Order has been placed |
+-----------------------+
4 rows in set (0.00 sec)

更新于:26-12-2019

580阅读

开启您的职业

完成课程并获得认证

开始
广告