Concatenate all the columns in a single new column with MySQL


Let us first create a −

mysql> create table DemoTable1396
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(40),
   -> Age int
   -> );
Query OK, 0 rows affected (0.93 sec)

Insert some records in the table using insert −

mysql> insert into DemoTable1396(Name,Age) values('Chris',21);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable1396(Name,Age) values('David',24);
Query OK, 1 row affected (0.34 sec)
mysql> insert into DemoTable1396(Name,Age) values('Bob',26);
Query OK, 1 row affected (0.40 sec)

Display all records from the table using select −

mysql> select * from DemoTable1396;

This will produce the following output −

+----+-------+------+
| Id | Name  | Age  |
+----+-------+------+
|  1 | Chris |   21 |
|  2 | David |   24 |
|  3 | Bob   |   26 |
+----+-------+------+
3 rows in set (0.00 sec)

Following is the query to concatenate columns in a single new column −

mysql> select Id,Name,Age, concat(Id,Name,Age) as AllColumns from DemoTable1396;

This will produce the following output −

+----+-------+------+------------+
| Id | Name  | Age  | AllColumns |
+----+-------+------+------------+
|  1 | Chris |   21 | 1Chris21   |
|  2 | David |   24 | 2David24   |
|  3 | Bob   |   26 | 3Bob26     |
+----+-------+------+------------+
3 rows in set (0.00 sec)

Updated on: 11-Nov-2019

187 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements