带有 SELECT 的 MySQL CASE WHEN 显示奇偶 ID?
我们首先创建一个表 −
mysql> create table DemoTable ( PageId int ); Query OK, 0 rows affected (0.85 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(233); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values(34); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(76); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(89); Query OK, 1 row affected (0.26 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+--------+ | PageId | +--------+ | 233 | | 34 | | 76 | | 89 | +--------+ 4 rows in set (0.00 sec)
以下是用 SELECT 的 CASE WHEN 查询 −
mysql> select PageId, case when PageId %2 !=0 then 'PageId is odd' else 'PageId is even' end AS Result from DemoTable;
这将产生以下输出 −
+--------+----------------+ | PageId | Result | +--------+----------------+ | 233 | PageId is odd | | 34 | PageId is even| | 76 | PageId is even| | 89 | PageId is odd| +--------+----------------+ 4 rows in set (0.00 sec)
广告