編寫 MySQL CASE 語句為學生的成績設定自定義訊息
對於此,使用 MySQL CASE 語句設定條件 -
mysql> create table DemoTable1916 ( StudentName varchar(20), StudentMarks int ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令在表中插入一些記錄 -
mysql> insert into DemoTable1916 values('Chris',59);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1916 values('David',89);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1916 values('Sam',94);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1916 values('Mike',75);
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1916 values('Carol',69);
Query OK, 1 row affected (0.00 sec)使用 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)
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP