
- 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
MySQL concatenation operator?
You can use in-built function CONCAT() from MySQL. The syntax is as follows −
SELECT CONCAT(('(',yourColumnName1,',', yourColumnName2,',',yourColumnName3,...N')')as anyVariableName from yourTableName;
To understand the above syntax, let us first create a table. The query to create a table is as follows −
mysql> create table Concatenation_OperatorDemo -> ( -> -> VendorId int, -> VendorName varchar(100), -> VendorCountry varchar(100) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into Concatenation_OperatorDemo values(101,'Carol','US'); Query OK, 1 row affected (0.19 sec) mysql> insert into Concatenation_OperatorDemo values(102,'Bob','UK'); Query OK, 1 row affected (0.18 sec) mysql> insert into Concatenation_OperatorDemo values(103,'John','US'); Query OK, 1 row affected (0.14 sec) mysql> insert into Concatenation_OperatorDemo values(104,'David','UK'); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from Concatenation_OperatorDemo;
The following is the output −
+----------+------------+---------------+ | VendorId | VendorName | VendorCountry | +----------+------------+---------------+ | 101 | Carol | US | | 102 | Bob | UK | | 103 | John | US | | 104 | David | UK | +----------+------------+---------------+ 4 rows in set (0.00 sec)
Now you can concat N number of columns using concat() function. Here, we will concat all three columns from the above table. The query to concat the column values are as follows −
mysql> select concat('(',VendorId,',', VendorName,',', VendorCountry,')')as ConcatenationDemo -> from Concatenation_OperatorDemo;
The following is the output −
+-------------------+ | ConcatenationDemo | +-------------------+ | (101,Carol,US) | | (102,Bob,UK) | | (103,John,US) | | (104,David,UK) | +-------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Equaivalent of Oracle concatenation operator in MySQL?
- String Concatenation by + (string concatenation) operator.
- How does concatenation operator work on list in Python?
- How does concatenation operator work on tuple in Python?
- How to do string concatenation without '+' operator in Python?
- Why should you be careful about String concatenation (+) operator in loops using Java?
- Using ! operator in MySQL
- MySQL rows concatenation to fetch maximum corresponding value from duplicate IDs?
- What is the operator in MySQL?
- Perform MySQL update with AND operator
- MySQL XOR operator with IN clause?
- UPDATE with logical AND operator in MySQL
- String Concatenation in Java
- How Can MySQL operator precedence affect result set?
- How Are MySQL INSTR() and LIKE operator similar?

Advertisements