
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Concatenate two columns in MySQL?
To concatenate two columns, use CONCAT() function in MySQL. The syntax is as follows −
select CONCAT(yourColumnName1, ' ',yourColumnName2) as anyVariableName from yourTableName;
To understand the above concept, let us create a table. The query to create a table is as follows −
mysql> create table concatenateTwoColumnsDemo −> ( −> StudentId int, −> StudentName varchar(200), −> StudentAge int −> ); Query OK, 0 rows affected (1.06 sec)
Now you can insert some records in the table. The query to insert records is as follows −
mysql> insert into concatenateTwoColumnsDemo values(1,'Sam',21); Query OK, 1 row affected (0.18 sec) mysql> insert into concatenateTwoColumnsDemo values(2,'David',24); Query OK, 1 row affected (0.17 sec) mysql> insert into concatenateTwoColumnsDemo values(3,'Carol',22); Query OK, 1 row affected (0.13 sec) mysql> insert into concatenateTwoColumnsDemo values(4,'Johnson',19); Query OK, 1 row affected (0.17 sec)
Display all records from the table with the help of select statement. The query is as follows −
mysql> select *from concatenateTwoColumnsDemo;
The following is the output −
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Sam | 21 | | 2 | David | 24 | | 3 | Carol | 22 | | 4 | Johnson | 19 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)
Implement the CONCAT() function to concatenate two columns. Here, we are concatenating columns StudentName and StudentAge. The query is as follows −
mysql> select CONCAT(StudentName, ' ',StudentAge) as NameAndAgeColumn from concatenateTwoColumnsDemo;
The following is the output displaying the concatenated columns −
+------------------+ | NameAndAgeColumn | +------------------+ | Sam 21 | | David 24 | | Carol 22 | | Johnson 19 | +------------------+ 4 rows in set (0.00 sec)
- Related Articles
- How to concatenate all columns in MySQL?
- Concatenate columns from different tables in MySQL
- Concatenate two columns when one of such column values is null in MySQL
- Concatenate all the columns in a single new column with MySQL
- Concatenate multiple rows and columns in a single row with MySQL
- Concatenate two tables in MySQL with a condition?
- Concatenate date and time from separate columns into a single column in MySQL
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- Swap data between two columns in MySQL?
- Sorted difference between two columns in MySQL?
- Select distinct values from two columns in MySQL?
- Select distinct combinations from two columns in MySQL?
- Custom sorting using two different columns in MySQL?
- Concatenate two values from the same column with different conditions in MySQL
- Order a MySQL table by two columns?

Advertisements