pogeybait
pogeybait

Reputation: 3145

How to use MySQL to return a concatenated string?

I am using a custom built version of data tables to list records from tables. Most of the time it is wonderful but occasionally I need to join two or more tables to show specific data from lookup tables. Here is a new problem. I have 3 tables... event_categories, themes, and themes_eventcategories. event_categories and themes are normal tables and themes_eventcategories has the fields 'id', 'theme_id', and 'event_category_id'.

What I need to do is get a list of event categories and if there are any themes associated with the event category, I need the themes in a comma separated string. Is this possible?

Upvotes: 1

Views: 48

Answers (1)

DarkAjax
DarkAjax

Reputation: 16223

What you want is GROUP_CONCAT, maybe something like:

SELECT event, GROUP_CONCAT(theme)
FROM themes_eventcategories a
JOIN event_categories b ON a.event_category_id = b.id
JOIN themes c ON a.theme_id = c.id
GROUP BY event

Upvotes: 1

Related Questions