Select multiple columns and display in a single column in MySQL?


Use concat() for this. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> FirstName varchar(30),
   -> LastName varchar(30)
   -> );
Query OK, 0 rows affected (0.49 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Chris','Brown');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('Adam','Smith');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('Carol','Taylor');
Query OK, 1 row affected (0.15 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| Chris     | Brown    |
| Adam      | Smith    |
| Carol     | Taylor   |
+-----------+----------+
3 rows in set (0.00 sec)

Following is the query to select multiple columns with single alias −

mysql> select concat(FirstName,' ',LastName) as concatValue from DemoTable order by
concatValue DESC;

Output

This will produce the following output −

+--------------+
| concatValue  |
+--------------+
| Chris Brown  |
| Carol Taylor |
| Adam Smith   |
+--------------+
3 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements