如何利用 MySQL 中的列舉值來構建表示式?


我們知道列舉值與索引值相關聯,因此如果我們在表示式中使用列舉值,則所有計算都將基於索引號進行。以下示例將對此進行說明

mysql> Select * from Result;
+-----+--------+-------+
| Id  | Name   | Grade |
+-----+--------+-------+
| 100 | Gaurav | GOOD  |
| 101 | Rahul  | POOR  |
| 102 | Rahul  | NULL  |
| 103 | Mohan  |       |
+-----+--------+-------+
4 rows in set (0.00 sec)

mysql> Select SUM(Grade) from result;
+------------+
| SUM(Grade) |
+------------+
|          3 |
+------------+
1 row in set (0.00 sec)

mysql> Select AVG(Grade) from result;
+------------+
| AVG(Grade) |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

mysql> Select Grade+0 from result;
+---------+
| Grade+0 |
+---------+
|       2 |
|       1 |
|    NULL |
|       0 |
+---------+
4 rows in set (0.00 sec)

mysql> Select Grade-1 from result;
+---------+
| Grade-1 |
+---------+
|       1 |
|       0 |
|    NULL |
|      -1 |
+---------+
4 rows in set (0.00 sec)

從上述查詢得到的結果顯示瞭如何在表示式中使用列舉值。

更新於: 2020 年 2 月 3 日

110 次瀏覽

開啟您的 職業生涯

完成課程獲取認證

開始學習
廣告
© . All rights reserved.