title: How to write SQL Query to join two tables with mappings table and get formatted output with only one row per result?
body: <p>I have three tables.</p>
  
  <hr>
  
  <pre><code>Table: "Foods"
  -----------
  ID    Name
  -----------
  1     Apple
  2     Strawberry
  3     Celery
  4     Peanuts
  5     Toffee
  </code></pre>
  
  <hr>
  
  <pre><code>Table: "Tags"
  ------------
  ID     Name
  ------------
  A      Sweet
  B      Fruit
  C      Crunchy
  </code></pre>
  
  <hr>
  
  <pre><code>Table: "Mappings"
  -------------------------
  ID    TagID     FoodID
  -------------------------
  101   A          1
  102   A          2
  103   A          5
  104   B          2   
  105   C          1
  106   C          3
  107   C          4
  108   C          5
  </code></pre>
  
  <p>I'm trying to run a query and get results that are formatted like this:</p>
  
  <pre><code>** DESIRED RESULTS OUTPUT FORMAT **
  ------------------------------------------------------
  ID    Name          IsSweet     IsFruit     IsCrunchy
  ------------------------------------------------------
  1     Apple               1           1             1
  2     Strawberry          1           1             0
  3     Celery              0           0             1
  4     Peanuts             0           0             1
  5     Toffee              1           0             1
  </code></pre>
  
  <p>Here is the query I'm currently using <strong>that is not working</strong> how I would like:</p>
  
  <pre><code>SELECT foods.id, foods.name, 
    IF(tags.name = "Sweet", 1, 0) IsSweet, 
    IF(tags.name = "Fruit", 1, 0) IsFruit,
    IF(tags.name="Crunchy", 1, 0) IsCrunchy,
    from Foods, Tags, Mappings 
      where Foods.ID = Mappings.FoodID and Mappings.TagID = Tags.ID
  </code></pre>
  
  <p>I'm getting results like the following, where every food item is repeated N times, once for each tag that exists.</p>
  
  <pre><code>** ACTUAL RESULTS I'M GETTING **
  ------------------------------------------------------
  ID    Name          IsSweet     IsFruit     IsCrunchy
  ------------------------------------------------------
  1     Apple               1           0             0
  1     Apple               0           1             0
  1     Apple               0           0             1
  2     Strawberry          1           0             0
  2     Strawberry          0           1             0
  3     Celery              0           0             1
  4     Peanuts             0           0             1
  5     Toffee              1           0             0
  5     Toffee              0           0             1
  </code></pre>
  
  <p>How can I write my query to get the results formatted how I would like? I've tried Outer Joins, and tried to use <code>CASE/WHEN</code> statements or <code> GROUP BY</code> but I haven't figured it out yet.</p>
  
  <p>Can anyone offer some direction for me please?</p>