MySQL 中 CASE WHEN 的替代方案

mysqlmysqli database更新于 2024/8/11 5:00:17

使用 IF() 方法作为 MySQL 中 CASE WHEN 的替代方案。让我们首先创建一个表 −

mysql> create table DemoTable1593
   -> (
   -> PlayerScore int
   -> );
Query OK, 0 rows affected (0.44 sec)

使用 insert 插入命令在表中插入一些记录 −

mysql> insert into DemoTable1593 values(78);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1593 values(0);
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1593 values(89);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable1593 values(0);
Query OK, 1 row affected (0.16 sec)

使用 select 语句显示表中的所有记录 −

mysql> select * from DemoTable1593;

这将产生以下输出 −

+-------------+
| PlayerScore |
+-------------+
|          78 |
|           0 |
|          89 |
|           0 |
+-------------+
4 rows in set (0.00 sec)

这是使用 MySQL IF() 的查询 −

mysql> select if(PlayerScore=0,'',PlayerScore) from DemoTable1593;

这将产生以下输出 −

+----------------------------------+
| if(PlayerScore=0,'',PlayerScore) |
+----------------------------------+
| 78                               |
|                                  |
| 89                               |
|                                  |
+----------------------------------+
4 rows in set (0.00 sec)


相关文章


有用资源