I have a standard many to many relationship between two tables, linked using a third linking table. Is there anyway to JOIN the linked ids into one record with a query?
I realize I can process a typical join and build a new list with this, just wondering if it can be done with a query. Example:
EventID | EventName | EventTypeIDs
1 | Party | 1,2,3
Events
+-------------+-------------+
| Field | Type |
+-------------+-------------+
| EventID | INT |
| EventName | varchar(45) |
+-------------+-------------+
EventTypes
+-------------+-------------+
| Field | Type |
+-------------+-------------+
| id | INT |
| value | varchar(25) |
+-------------+-------------+
EventTypeLink
+-------------+-------------+
| Field | Type |
+-------------+-------------+
| id | INT |
| EventID | INT |
| EventTypeID | INT |
+-------------+-------------+
Yes there is. Use the
GROUP_CONCAT()like so:SQL Fiddle Demo
This will give you:
Note that: If you need to inclue those events with no types, use a
LEFT JOINinstead, withIFNULLlike so:SQL Fiddle Demo for those with no types
This will give you something like: