为此,请使用MySQL CASE语句设置条件-
mysql> create table DemoTable1916 ( StudentName varchar(20), StudentMarks int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1916 values('Chris',59); mysql> insert into DemoTable1916 values('David',89); mysql> insert into DemoTable1916 values('Sam',94); mysql> insert into DemoTable1916 values('Mike',75); mysql> insert into DemoTable1916 values('Carol',69);
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1916;
这将产生以下输出-
+-------------+--------------+ | StudentName | StudentMarks | +-------------+--------------+ | Chris | 59 | | David | 89 | | Sam | 94 | | Mike | 75 | | Carol | 69 | +-------------+--------------+ 5 rows in set (0.00 sec)
这是为学生分数设置自定义消息的查询-
mysql> select StudentName, case when StudentMarks > 70 Then 'Good Marks' else 'Not Good Marks' end as Result from DemoTable1916;
这将产生以下输出-
+-------------+----------------+ | StudentName | Result | +-------------+----------------+ | Chris | Not Good Marks | | David | Good Marks | | Sam | Good Marks | | Mike | Good Marks | | Carol | Not Good Marks| +-------------+----------------+ 5 rows in set (0.00 sec)