MySQL Group Concat Function

The group_concat() function in MySQL is really nifty. It concatenates all rows that match your group by statement into a single, comma separated string.

Essentially, it transposes rows to columns.

Take this example. Say you have a table that stores a set of locations by country, state and city.

You could then write SQL as follows:

select 
country,
state,
group_concat(city) as cities
from locations
group by country, state

What this is saying is bring back 3 columns, country, state, and for each combination of country and state, a list of all of the cities in that country and state as the third column (called cities).

This is actually a phenomenally handy thing to be able to do and something you’ll use a lot once you know it exists!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Cloud Watch and Cloud Trail Overview

How to discover up to 10,000 subdomains with your own tool

GSF Creator — Impressed — SFX Epic (4k)

GSF Creator - Impressed - SFX Epic (4k)

Bromite Browser APK Download (Latest Version) for Android

History of the Web Application

(Functional) Object Oriented Programming With Message Passing

ApacheCon Europe 2019 — Thoughts and Insights by Airflow Committers

Coding a simple Csv Reader in C# and Blazor

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Praj Basnet

Praj Basnet

More from Medium

What is MongoDB?

Creating and handling MongoDB Documents

MySQL Shutdown Unexpectedly Error

How to search null value in Elasticsearch