Mysql

Concatenate Multiple Columns in MySQL

in this tutorial, We’ll concatenate two or multiple columns in MySQL.We will select the values and concat multiple columns using MySQL inbuilt method.

It can also achieve the same thing using programmatically. You need to select columns fields value separately from MySQL Table and store their values in the single variable after concat their values.

Case 1: I am assuming, We have firstname, lastname column within our DataBase Table. Concatenate firstname, lastname columns values in a single string form.

There are two functions for doing this –

  • CONCAT
  • CONCAT_WS

Above function work for concate multiple columns :

Related Post

CONCAT

This function is used to concatenate multiple columns or strings into a single one. The method arguments are separated by a comma.

Syntax –

CONCAT( column1, column2, … )
OR
CONCAT ( string1, string2, … )

MySQL Query to Concatenate table Columns

SELECT 
   CONCAT( firstname, " ", lastname ) AS fullname, salary
   FROM employees

CONCAT_WS

The CONCAT_WS() function is used to add multiple string values and makes them a single string value. The main difference between CONCAT() and CONCAT_WS() is that we can define separator. It also let you define separator ( ” “, “, “, ” – “, etc.).

Syntax –

CONCAT_WS( SEPARATOR, column1, column2, … )
OR
CONCAT ( SEPARATOR, string1, string2, … )

Example concatenate Using CONCAT_WS()

SELECT 
username,
CONCAT_WS( " ", firstname, lastname ) AS fullname
FROM users

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories