MySQL Concat Multiple Columns into One |
X

Congrats, You are Subscribed to Receive Updates.

MySQL Concat Multiple Columns into One


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.

MySQL-Concat-Multiple-Columns-into-One

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.

commenter

About Varadharaj V

The founder of Kvcodes, Varadharaj V is an ERP Analyst and a Web developer specializing in WordPress(WP), WP Theme development, WP Plugin development, Frontaccounting(FA), Sales, Purchases, Inventory, Ledgers, Payroll & HRM, CRM, FA Core Customization, PHP and Data Analyst. Database Management Advance Level

Comment Below

Your email address will not be published. Required fields are marked *

*

Current ye@r *

Menu

Sidebar