MySQL CONCAT a specific column value with the corresponding record


Let us first create a table −

mysql> create table DemoTable
(
   FirstName varchar(100),
   CountryName varchar(100)
);
Query OK, 0 rows affected (0.56 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Adam','US');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('David','AUS');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('Chris','UK');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('John','US');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('Carol','AUS');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+-------------+
| FirstName | CountryName |
+-----------+-------------+
| Adam      | US          |
| David     | AUS         |
| Chris     | UK          |
| John      | US          |
| Carol     | AUS         |
+-----------+-------------+
5 rows in set (0.00 sec)

Following is the query to concatenate a specific column value with the corresponding record −

mysql> select concat(FirstName,' ',CountryName) AS NameWithCountry from DemoTable
having NameWithCountry LIKE '%John%';

This will produce the following output −

+-----------------+
| NameWithCountry |
+-----------------+
| John US         |
+-----------------+
1 row in set (0.00 sec)

Updated on: 26-Sep-2019

68 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements