让我们首先创建一个表-
mysql> create table DemoTable ( UserId int, UserName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(100,'Chris'); mysql> insert into DemoTable values(101,'Robert'); mysql> insert into DemoTable values(102,'David'); mysql> insert into DemoTable values(103,'Mike');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+----------+ | UserId | UserName | +--------+----------+ | 100 | Chris | | 101 | Robert | | 102 | David | | 103 | Mike | +--------+----------+ 4 rows in set (0.00 sec)
以下是CASE WHEN的查询-
mysql> select case when exists(select * from DemoTable where UserId=102) then 'Present' else 'Not Present' end as Result;
这将产生以下输出-
+---------+ | Result | +---------+ | Present | +---------+ 1 row in set (0.02 sec)