1

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?

4
  • This is possible, but do you use this query for production purposes? Commented Jul 29, 2013 at 18:36
  • It will be something we use on the back-end for administration so it is okay if it is a slow query. Commented Jul 29, 2013 at 18:49
  • It is not about speed. If you use concat and have lots of events, then there will be a problem. There is a certain amount of characters that you can use in that field. The best way is to use nested select Commented Jul 29, 2013 at 18:51
  • Also keep in mind that since I am using a custom version of datatables, I will need to pull data primarily from the event_categories table i.e. SELECT * FROM event_categories INNER JOIN themes_eventcategories .... etc etc Commented Jul 29, 2013 at 18:52

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.