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 –
Above function work for concate multiple columns :
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, … )
SELECT CONCAT( firstname, " ", lastname ) AS fullname, salary FROM employees
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, … )
SELECT username, CONCAT_WS( " ", firstname, lastname ) AS fullname FROM users
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More