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


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.

Updated on: 03-Feb-2020

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements