MySQL CASE WHEN 在 MySQL 中的替代方案
使用 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)
廣告