Skip to content

Latest commit

 

History

History
23 lines (18 loc) · 953 Bytes

use-group-concat-to-group-results.md

File metadata and controls

23 lines (18 loc) · 953 Bytes

Use group_concat in MySQL to concatenate group results into a single string

Group concat concatenates strings from a group into a single string.

Say you have node to tag relationship of many to many and you want to return a result where one column lists all the tags associated with a node as a comma-separated list.

SELECT DISTINCT node.title, group_concat(taxonomy_term_data.name separator ', ') AS tags
FROM node
INNER JOIN taxonomy_index
ON taxonomy_index.nid = node.nid
INNER JOIN taxonomy_term_data
ON taxonomy_index.tid = taxonomy_term_data.tid
GROUP BY node.nid

The output will be in this format:

title, tags
'First node title', 'First taxonoy term name, Second taxonomy term name'

Official Documentation, Stackoverflow post