- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to concatenate more than 2 fields with SQL?
To concatenate more than 2 fields with SQL, you can use CONCAT() or CONCAT_WS() function. The syntax is as follows. Let us first see using CONCAT().
SELECT CONCAT(yourColumnName1,'/',yourColumnName2, '/',yourColumnName3, '/',......N) AS anyVariableName FROM yourTableName;
The syntax is as follows:
SELECT CONCAT_WS(‘/’,yourColumnName1,yourColumnName2,.....N) AS anyVariableName FROM yourTableName;
To understand the above syntax, let us create a table. The query to create a table is as follows:
mysql> create table MoreThan2ColumnConcat -> ( -> Id int, -> Name varchar(20), -> Age int, -> Marks int -> ); Query OK, 0 rows affected (2.59 sec)
Insert some records in the table using insert command. The query is as follows:
mysql> insert into MoreThan2ColumnConcat values(1,'John',24,89); Query OK, 1 row affected (0.17 sec) mysql> insert into MoreThan2ColumnConcat values(11,'Larry',25,90); Query OK, 1 row affected (0.21 sec) mysql> insert into MoreThan2ColumnConcat values(15,'Mike',26,79); Query OK, 1 row affected (0.13 sec) mysql> insert into MoreThan2ColumnConcat values(16,'Sam',21,99); Query OK, 1 row affected (0.14 sec)
Now you can display all records from the table using select statement. The query is as follows:
mysql> select *from MoreThan2ColumnConcat;
The following is the output:
+------+-------+------+-------+ | Id | Name | Age | Marks | +------+-------+------+-------+ | 1 | John | 24 | 89 | | 11 | Larry | 25 | 90 | | 15 | Mike | 26 | 79 | | 16 | Sam | 21 | 99 | +------+-------+------+-------+ 4 rows in set (0.00 sec)
Here is the query to concatenate more than two fields with CONCAT().
mysql> select concat(Id,'/',Name, '/',Age, '/',Marks) as ConcatMoreFields from MoreThan2ColumnConcat;
The following is the output:
+------------------+ | ConcatMoreFields | +------------------+ | 1/John/24/89 | | 11/Larry/25/90 | | 15/Mike/26/79 | | 16/Sam/21/99 | +------------------+ 4 rows in set (0.00 sec)
Let us see the query to concatenate more than two fields using CONCAT_WS().
mysql> select concat_ws('/',Id,Name,Age,Marks) as ConcatMoreFields from MoreThan2ColumnConcat;
The following is the output:
+------------------+ | ConcatMoreFields | +------------------+ | 1/John/24/89 | | 11/Larry/25/90 | | 15/Mike/26/79 | | 16/Sam/21/99 | +------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Python - How to Concatenate more than two Pandas DataFrames?
- How to delete fields with values more than a particular value in MySQL?
- How to concatenate fields in MySQL?
- MongoDB query to concatenate values of array with other fields
- Concatenate fields in MongoDB?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- How to concatenate two or more vectors in R?
- Python - How to Concatenate Two or More Pandas DataFrames along rows?
- How to identify the SQL consuming more resources in Oracle?
- Python - How to Concatenate Two or More Pandas DataFrames along columns?\n
- Using the number line, write the integer which is(i) 4 more than 6(ii) 5 more than ( -6 )(iii) 6 less than 2(iv) 2 less than ( -3 )
- Using the number line write the integer which is :(a) 3 more than 5(b) 5 more than $–5$(c) 6 less than 2(d) 3 less than $–2$
- Add more than one shadow to a text with CSS
- Using the number line represent -2 more than -4.
- Prefixes with more a than b in C++

Advertisements