Search code examples
mysqlsqlgroup-byleft-join

MySQL: How do I join same table multiple times?


I have two tables ticket and attr. Table ticket has ticked_id field and several other fields. Table attr has 3 fields:

ticket_id - numeric
attr_type - numeric
attr_val - string

attr_type is a fixed enum of values. For example, it can be 1, 2 or 3.

I need to make a query, the result of which will be 4 columns:

ticket_id, attr_val for attr_type=1, attr_val for attr_type=2, attr_val for attr_type=3

If there is no corresponding value for attr_type in attr table then NULL value should be shown in corresponding column.

ticket table :

ticket_id
1
2
3

attr table :

ticket_id attr_type attr_val
1 1 Foo
1 2 Bar
1 3 Egg
2 2 Spam

Expected output :

ticket_id attr_val1 attr_val2 attr_val3
1 Foo Bar Egg
2 null Spam null
3 null null null

I tried left joining attr table 3 times, but cannot figure out how to arrange output by attr_type


Solution

  • You need to use multiple LEFT JOINs:

    SELECT 
        ticket.ticket_id,  
        a1.attr_val AS attr_val1,
        a2.attr_val AS attr_val2,
        a3.attr_val AS attr_val3
    FROM ticket
        LEFT JOIN attr a1 ON ticket.ticket_id=a1.ticket_id AND a1.attr_type=1
        LEFT JOIN attr a2 ON ticket.ticket_id=a2.ticket_id AND a2.attr_type=2
        LEFT JOIN attr a3 ON ticket.ticket_id=a3.ticket_id AND a3.attr_type=3
    

    Here is an example: SQL Fiddle.