Tutorialspoint

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


1 Answer
karthikeya Boyini

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;

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;

This will produce the following output −

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.