- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements