
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- 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?
- How to round MySQL column with float values and display the result in a new column?
- MySQL query to display only the column values with corresponding column having whitespace
- MySQL query to increment one of the column values
- Query to divide the values of two columns and display the result in a new column using MySQL wildcard?
- MySQL concat() to create column names to be used in a query?
- Make all column names lower case in MySQL with a single query
- How to convert a column values to column names in R?
- MySQL query to sum 3 different values in a column displaying total of each value in result set?
- Select distinct names from two columns in MySQL and display the result in a single column
- 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 group by column and display the sum of similar values in another column
- MySQL query to remove special characters from column values?

Advertisements