为此,请使用IF()
。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> PlayerName varchar(100), -> PlayerScore int, -> PlayerStatus varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('John',88,'BAD'); mysql> insert into DemoTable values('Chris',78,'BAD'); mysql> insert into DemoTable values('Robert',90,'BAD'); mysql> insert into DemoTable values('David',80,'BAD');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+------------+-------------+--------------+ | PlayerName | PlayerScore | PlayerStatus | +------------+-------------+--------------+ | John | 88 | BAD | | Chris | 78 | BAD | | Robert | 90 | BAD | | David | 80 | BAD | +------------+-------------+--------------+ 4 rows in set (0.00 sec)
以下是针对第三个字段的值从两个字段中选择一个字段的查询-
mysql> select PlayerScore,if(PlayerScore > 80 , PlayerName ,PlayerStatus) AS Result from DemoTable;
输出结果
+-------------+--------+ | PlayerScore | Result | +-------------+--------+ | 88 | John | | 78 | BAD | | 90 | Robert | | 80 | BAD | +-------------+--------+ 4 rows in set (0.00 sec)