MySQL Concat Multiple Columns into One
- Article
- Comment
MySQL Concat Multiple Columns into One. There are two possibilities while writing query to combine multiple columns.
- Two columns
- More than Two columns
Let’s see each one in detail.
Two Columns
Suppose, if you have the need to combine two columns into one and with some concatenation thing. Say for example, sometimes we need to show first and last name of an user from the users table.
In that case, we can use CONCAT
to combine those two columns into one.
SELECT id, CONCAT(lastname, ', ', firstname) AS fullname FROM `users`;
From the above query we can get first and lastname together as fullname.
More Than Two Columns
For multiple columns to merge and get single column CONCAT_WS
. Let’s see an example with it. Suppose if we have first, middle, and last name in users table. We can combine all in one column with it.
SELECT id, CONCAT_WS(',', firstname, middlename, lastname) AS fullname FROM `users`;
Thats the difference between two columns and multiple columns combine results. If you are looking to combine multiple row records, Take a look at here.
If you really enjoy this article you can subscribe my newsletter. Please use the sidebar to get future article updates from me. Also you can follow and subscribe me on social sites. Hope you will enjoy reading more articles from me.