How an enumeration value in MySQL can be used in an expression?

MySQLMySQLi Database

As we know that enumeration values are associated with index values hence if we will use enumeration values in an expression then all the calculations would be done on index numbers. The following example will clarify it −

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)

The result from above query shows how enumeration values can be used in an expression.

raja
Published on 20-Feb-2018 11:13:10
Advertisements