Reputation: 753
Does anyone knows how to do a mysql inner join with multiple values from a table in a column, I mean:
t_1:
id_t1 | name
001 | name_value
t_2:
id_t2 | value
020 | value1
030 | value2
040 | value3
050 | value4
t1_t2:
id_t1 | id_t2
001 | 020
001 | 030
001 | 050
Then, a query that return me for example, something like this:
id_t1 | name
| values_t_2
001 | name_value | value1, value2, value4
If anyone can tell me a way to do this, I'd be grateful.
Upvotes: 1
Views: 1291
Reputation: 13328
SELECT
t1.id_t1,
t1.name,
GROUP_CONCAT(t2.value SEPARATOR ', ') AS values_t_2
FROM
t_1 t1
INNER JOIN t1_t2 t1t2 ON (t1.id_t1 = t1t2.id_t1)
INNER JOIN t_2 t2 ON (t1t2.id_t2 = t2.id_t2)
GROUP BY
t1.id_t1
Upvotes: 5