简体   繁体   中英

MySQL INNER JOIN with multiple values in a column

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 id_t1 | name
001 | name_value

t_2: id_t2 | value id_t2 | value
020 | value1
030 | value2
040 | value3
050 | value4

t1_t2: id_t1 | id_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 id_t1 | name | values_t_2 | values_t_2
001 | name_value | value1, value2, value4

If anyone can tell me a way to do this, I'd be grateful.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM