How to map keys to values for an individual field in a MySQL select query?


You can use CASE statement in MySQL to map keys to values for an individual field in select query. The syntax is as follows −

SELECT yourColumnName1,yourColumnName2,yourColumnName3,.........N
(
   CASE WHEN yourColumnName = 1 THEN 'ENABLED'
   ELSE 'DISABLED'
   END
) AS anyVariableName
FROM yourTableName;

You can use IF() function also for the same purpose. The syntax is as follows −

SELECT yourColumnName1,yourColumnName2,yourColumnName3,.........N
,IF(yourColumnName,'ENABLED','DISABLED') as anyVariableName FROM yourTableName;

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table MapKeys
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> isActive boolean,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.65 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into MapKeys(Name,isActive) values('Larry',true);
Query OK, 1 row affected (0.13 sec)

mysql> insert into MapKeys(Name,isActive) values('David',false);
Query OK, 1 row affected (0.16 sec)

mysql> insert into MapKeys(Name,isActive) values('Mike',true);
Query OK, 1 row affected (0.14 sec)

mysql> insert into MapKeys(Name,isActive) values('Carol',false);
Query OK, 1 row affected (0.17 sec)

mysql> insert into MapKeys(Name,isActive) values('Sam',false);
Query OK, 1 row affected (0.15 sec)

mysql> insert into MapKeys(Name,isActive) values('Bob',true);
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement. The query is as follows:

mysql> select *from MapKeys;

The following is the output −

+----+-------+----------+
| Id | Name  | isActive |
+----+-------+----------+
|  1 | Larry |        1 |
|  2 | David |        0 |
|  3 | Mike  |        1 |
|  4 | Carol |        0 |
|  5 | Sam   |        0 |
|  6 | Bob   |        1 |
+----+-------+----------+
6 rows in set (0.00 sec)

Let us now map key using case statement. The query is as follows −

mysql> select Id,Name,
   -> (
   -> CASE WHEN isActive = 1 THEN 'ENABLED'
   -> ELSE 'DISABLED'
   -> END
   -> ) AS Status
   -> from MapKeys;

The following is the output −

+----+-------+----------+
| Id | Name  | Status   |
+----+-------+----------+
|  1 | Larry | ENABLED  |
|  2 | David | DISABLED |
|  3 | Mike  | ENABLED  |
|  4 | Carol | DISABLED |
| 5  | Sam   | DISABLED |
| 6  | Bob   | ENABLED  |
+----+-------+----------+
6 rows in set (0.00 sec)

You can achieve the same with the help of IF() function −

mysql> select Id,Name,if(isActive,'ENABLED','DISABLED') as Status from MapKeys;

The following is the output −

+----+-------+----------+
| Id | Name  | Status   |
+----+-------+----------+
| 1 | Larry | ENABLED   |
| 2 | David | DISABLED |
| 3 | Mike   | ENABLED |
| 4 | Carol | DISABLED |
| 5 | Sam   | DISABLED |
| 6 | Bob   | ENABLED  |
+----+-------+----------+
6 rows in set (0.00 sec)

Updated on: 30-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements