

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Setting column values as column names in the MySQL query result?
To set column values as column names in the query result, you need to use a CASE statement.
The syntax is as follows −
select yourIdColumnName, max(case when (yourColumnName1='yourValue1') then yourColumnName2 else NULL end) as 'yourValue1', max(case when (yourColumnName1='yourValue2') then yourColumnName2 else NULL end) as 'yourValue2', max(case when yourColumnName1='yourValue3') then yourColumnName2 else NULL end) as 'yourValue3’, . . N from valueAsColumn group by yourIdColumnName order by yourIdColumnName;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table valueAsColumn -> ( -> UserId int, -> UserColumn1 varchar(10), -> UserColumn2 varchar(10) -> ); Query OK, 0 rows affected (0.75 sec)
Now you can insert some records in the table using insert command. The query is as follows −
mysql> insert into valueAsColumn values(0,'John','A+'); Query OK, 1 row affected (0.18 sec) mysql> insert into valueAsColumn values(0,'Carol','B'); Query OK, 1 row affected (0.17 sec) mysql> insert into valueAsColumn values(0,'Sam','C'); Query OK, 1 row affected (0.17 sec) mysql> insert into valueAsColumn values(1,'John','D'); Query OK, 1 row affected (0.20 sec) mysql> insert into valueAsColumn values(1,'Carol','A'); Query OK, 1 row affected (0.20 sec) mysql> insert into valueAsColumn values(1,'Carol','C'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from valueAsColumn;
The following is the output −
+--------+-------------+-------------+ | UserId | UserColumn1 | UserColumn2 | +--------+-------------+-------------+ | 0 | John | A+ | | 0 | Carol | B | | 0 | Sam | C | | 1 | John | D | | 1 | Carol | A | | 1 | Carol | C | +--------+-------------+-------------+ 6 rows in set (0.00 sec)
Here is the query to set column value as column names −
mysql> select UserId, -> max(case when (UserColumn1='John') then UserColumn2 else NULL end) as 'John', -> max(case when (UserColumn1='Carol') then UserColumn2 else NULL end) as 'Carol', -> max(case when (UserColumn1='Sam') then UserColumn2 else NULL end) as 'Sam' -> from valueAsColumn -> group by UserId -> order by UserId;
The following is the output −
+--------+------+-------+------+ | UserId | John | Carol | Sam | +--------+------+-------+------+ | 0 | A+ | B | C | | 1 | D | C | NULL | +--------+------+-------+------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- MySQL query to count the duplicate ID values and display the result in a separate column
- MySQL query to count occurrences of distinct values and display the result in a new column?
- MySQL query to increment one of the column values
- MySQL query to display only the column values with corresponding column having whitespace
- How to round MySQL column with float values and display the result in a new column?
- How to convert a column values to column names in R?
- How to display column values as CSV in MySQL?
- MySQL concat() to create column names to be used in a query?
- Make all column names lower case in MySQL with a single query
- Select distinct names from two columns in MySQL and display the result in a single column
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- MySQL UPDATE column names and set None values with N/A?
- MySQL query to group by names and display the count in a new column
- Set 'alias' for all the column names in a single MySQL query
- MySQL query to remove special characters from column values?
Advertisements