title: MySQL: How do I join same table multiple times?
body: <p>I have two tables <code>ticket</code> and <code>attr</code>. Table <code>ticket</code> has <code>ticked_id</code> field and several other fields. Table <code>attr</code> has 3 fields:</p>
  
  <pre><code>ticket_id - numeric
  attr_type - numeric
  attr_val - string
  </code></pre>
  
  <p><code>attr_type</code> is a fixed enum of values. For example, it can be <code>1</code>, <code>2</code> or <code>3</code>.</p>
  
  <p>I need to make a query, the result of which will be 4 columns:</p>
  
  <p><code>ticket_id</code>, <code>attr_val</code> for <code>attr_type=1</code>, <code>attr_val</code> for <code>attr_type=2</code>, <code>attr_val</code> for <code>attr_type=3</code></p>
  
  <p>If there is no corresponding value for <code>attr_type</code> in <code>attr</code> table then NULL value should be  shown in corresponding column.</p>
  
  <p>Example:</p>
  
  <pre><code>ticket
      ticket_id: 1
  
      ticket_id: 2
  
      ticket_id: 3
  
  
  attr
      ticket_id: 1
      attr_type: 1
      attr_val: Foo
  
      ticket_id: 1
      attr_type: 2
      attr_val: Bar
  
      ticket_id: 1
      attr_type: 3
      attr_val: Egg
  
      ticket_id: 2
      attr_type: 2
      attr_val: Spam
  </code></pre>
  
  <p>the result should be:</p>
  
  <pre><code>ticked_id: 1
  attr_val1: Foo
  attr_val2: Bar
  attr_val3: Egg
  
  ticked_id: 2
  attr_val1: NULL
  attr_val2: Spam
  attr_val3: NULL
  
  ticked_id: 3
  attr_val1: NULL
  attr_val2: NULL
  attr_val3: NULL
  </code></pre>
  
  <p>I tried left joining <code>attr</code> table 3 times, but cannot figure out how to arrange output by <code>attr_type</code></p>