How to concatenate two column values into a single column with MySQL. The resultant column values should be separated by hyphen

MySQLMySQLi Database

Let us first create a table −

mysql> create table DemoTable835(
   CountryCode int,
   CountryName varchar(100)
);
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable835 values(100,'US');
Query OK, 1 row affected (0.36 sec)
mysql> insert into DemoTable835 values(101,'UK');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable835 values(102,'AUS');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable835 values(103,'ENG');
Query OK, 1 row affected (0.09 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable835;

This will produce the following output −

+-------------+-------------+
| CountryCode | CountryName |
+-------------+-------------+
|         100 | US          |
|         101 | UK          |
|         102 | AUS         |
|         103 | ENG         |
+-------------+-------------+
4 rows in set (0.00 sec)

Following is the query to concatenate two columns values into a single column−

mysql> select concat(CountryCode,'-',CountryName) from DemoTable835;

This will produce the following output −

+-------------------------------------+
| concat(CountryCode,'-',CountryName) |
+-------------------------------------+
| 100-US                              |
| 101-UK                              |
| 102-AUS                             |
| 103-ENG                             |
+-------------------------------------+
4 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 16:21:14
Advertisements