How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Combine Multiple Columns into One String in MySQL
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Combine Multiple Columns into One String in MySQL
Web Development

How to Combine Multiple Columns into One String in MySQL

how7o
By how7o
Last updated: May 10, 2026
6 Min Read
MySQL combine columns into string — CONCAT and CONCAT_WS
SHARE

To mysql combine columns into string like an address or a full name, use CONCAT (explicit separators) or CONCAT_WS (one shared separator that’s repeated, NULL-aware). Both are built into MySQL and MariaDB — no PHP-side post-processing needed. This guide covers both, when to use which, the NULL behavior that catches people out, and when to store the concatenated value for indexed filtering.

Contents
  • TL;DR
  • CONCAT — explicit separators
  • CONCAT_WS — shared separator, NULL-aware
  • Inside Laravel / WordPress queries
  • Indexing and performance
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on MySQL 8.0 and MariaDB 10.11. Originally published 2022-11-06, rewritten and updated 2026-04-23.

TL;DR

-- CONCAT_WS — one shared separator, skips NULLs
SELECT
    id,
    CONCAT_WS(', ', house_no, street, city, country) AS address
FROM user_details;

-- CONCAT — explicit separator per join, NULL-sensitive
SELECT
    id,
    CONCAT(first_name, ' ', last_name) AS full_name
FROM users;

CONCAT — explicit separators

SELECT
    id,
    CONCAT(house_no, ', ', street, ', ', city, ', ', country) AS address
FROM user_details;

CONCAT joins every argument in order. You write each separator inline (', '), giving you full control over what sits between each column. Useful when the separators differ: CONCAT(first_name, ' ', last_name, ', ', title).

Caveat: if any argument is NULL, the whole result is NULL. For nullable columns you need to COALESCE each one: CONCAT(COALESCE(col1, ''), ', ', COALESCE(col2, '')). Quickly tedious.

CONCAT_WS — shared separator, NULL-aware

SELECT
    id,
    CONCAT_WS(', ', house_no, street, city, country) AS address
FROM user_details;

CONCAT_WS(separator, arg1, arg2, ...) — “concatenate with separator.” The separator is the first argument; subsequent arguments are joined with it in between. Two advantages over CONCAT:

  • No need to repeat the separator — write it once.
  • NULL values are skipped automatically. A row with house_no = NULL still produces a clean "Main St, Dhaka, BD" instead of NULL.

For addresses, names, breadcrumbs, tag lists — anywhere you’re joining with the same separator — CONCAT_WS is the one to reach for.

mysql combine columns into string — CONCAT vs CONCAT_WS with NULL handling

Inside Laravel / WordPress queries

Reaching for CONCAT inside Eloquent or $wpdb runs into the same gotcha: the ORM’s select() escapes identifiers, so plain concatenation strings get treated as column names. Wrap in DB::raw() or use selectRaw():

// Laravel
use Illuminate\Support\Facades\DB;

User::selectRaw("id, CONCAT_WS(', ', house_no, street, city, country) AS address")
    ->where('user_id', $userId)
    ->get();

See the full walkthrough in the “Unknown column ‘CONCAT'” fix in Laravel.

Indexing and performance

Concatenation is computed at query time, so WHERE CONCAT_WS(',', street, city) LIKE '%Dhaka%' forces a full table scan — the database has to build the string for every row before comparing. Two fixes:

  • Filter on the source columns instead — WHERE city = 'Dhaka' uses an index on city; WHERE CONCAT_WS(...) LIKE '%Dhaka%' does not.
  • Store and index — add a VARCHAR column (or a MySQL 8.0 generated column), populate with the concatenation, index it. Now the lookup is O(log n).
-- Generated column (MySQL 8+)
ALTER TABLE user_details
ADD COLUMN full_address VARCHAR(512)
    GENERATED ALWAYS AS (CONCAT_WS(', ', house_no, street, city, country))
    STORED,
ADD INDEX idx_full_address (full_address);

The value auto-maintains as the source columns change, and WHERE full_address LIKE 'Dhaka%' now uses the index.

Frequently asked questions

CONCAT or CONCAT_WS — which should I use for mysql combine columns into string?

CONCAT_WS (‘with separator’) is the right default when you’re joining with the same separator — addresses, names, breadcrumb paths. CONCAT is better when each separator is different (e.g. CONCAT(first, ' ', last, ', ', title)). CONCAT_WS also automatically skips NULL values, where CONCAT returns NULL if any argument is NULL.

What happens when a column is NULL?

CONCAT('A', NULL, 'B') returns NULL — the whole expression. CONCAT_WS(',', 'A', NULL, 'B') returns 'A,B' — NULL values are skipped silently. For address strings where a row might be missing address_2, this is exactly what you want. For cases where you want to substitute a default instead of skipping, wrap with COALESCE: CONCAT_WS(',', col1, COALESCE(col2, 'N/A')).

Does concatenation use an index?

No — computing a concatenation requires reading every selected row, so filtering by the concatenated value forces a full table scan. If you filter on addresses frequently, store the composed address in a column (or a generated column) and index that. See the generated-column pattern for the stored-computed approach.

How do I concatenate with a newline?

CONCAT_WS(CHAR(10), line1, line2, line3) — CHAR(10) is ASCII line feed. For a CRLF pair (Windows-style): CHAR(13, 10). Usable anywhere you’d put a literal separator.

Can I use GROUP_CONCAT instead?

Different tool for a different problem. GROUP_CONCAT concatenates values across rows in a group — useful for ‘give me a comma-separated list of tags for each post.’ CONCAT/CONCAT_WS works within a single row. For combining columns of one row into one string, stick with CONCAT_WS.

Related guides

  • How to Fix “Unknown column ‘CONCAT'” in Laravel — using CONCAT from Eloquent.
  • How to Extract Only the Digits from a String in MySQL — sibling string-function pattern.
  • How to Remove a Specific String from a Column in MySQL — other string transforms.
  • How to Prepare a %LIKE% SQL Statement in WordPress — pairing with LIKE searches.

References

MySQL CONCAT and CONCAT_WS: dev.mysql.com/doc/refman/8.0/en/string-functions.

TAGGED:mysqlsql

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article MySQL top CPU usage — PROCESSLIST snapshot and performance_schema digest How to Check Which MySQL Database or User Is Using the Most CPU
Next Article MySQL connect remote from Ubuntu — mysql-client + mysql -h host How to Connect to a Remote MySQL Database from Ubuntu
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Laravel Eloquent records today — Carbon today helper and whereDate illustration
Web Development

How to Get Records Created Today in Laravel

6 Min Read
Duplicate a div into another div with jQuery clone
Web Development

How to Duplicate a DIV into Another DIV with jQuery

4 Min Read
Laravel migration adding two new columns to an existing transactions table
Web Development

How to Add New Columns to an Existing Table in Laravel Migration

5 Min Read
Create custom exception class in Laravel (Artisan command + secure error handling)
Web Development

How to Create a Custom Exception Class in Laravel (With Clean JSON Responses)

6 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?