1

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.

1 Answer 1

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

3 Comments

@Sebas There is no standard SQL way to do this. Each SQL server implements its own mechanism.
this is correct, as of the group_concat thing. But I was refering to the GROUP BY part.
Because t1.name isn't wrapped in an aggregate function and therefore wouldn't work on most SQL servers?

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.