How to concatenate columns in MySql

There are many times you need to concatenate the values of two or more columns while querying the result. To concatenate columns in MySql there is in-built function for that.

Let’s take a simple scenario where you store firstname and lastname of user in some table. Now you want to concatenate the result of firstname and lastname and show them as a name.

To concatenate two values in mysql. Mysql provides concat function.

How to concatenate column in MySql

Let’s i explain this.

First create table name users. In user table i create three columns id, firstname , lastname.

Insert some dummy values.

After running the above queries, table is created and it look like.

id firstname lastname
1 John Doe
2 John Smith

Syntax of concat function-

concat(col1,”,col2);

Output

user_name
John Doe
John Smith

CONCAT_WS to Concatenate Columns with Separator

Syntax-

concat_ws functon concatenates the value with separator you specify.

It returns the concatenate value of firstname and lastname with comma separated.

Tagged , . Bookmark the permalink.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.