title: MySQL LEFT JOIN - How to handle cases where records have no match with PHP
body: <p>I have a HTML table populated with records returned from MySQL <code>LEFT JOIN</code>. </p>
  
  <p>This works, but I need to handle records that do not have a match in the <code>JOIN</code>.  Currently, the HTML table is like the illustration below, and unit_outcome records that are unmatched sit in a cell with no corresponding program_outcome.</p>
  
  <pre><code>+----------------------------------+--------------------------------------------------+
  | program_outcome                  |  unit_outcome                                    |
  +----------------------------------+--------------------------------------------------+
  |                                  | unit_outcome 5, unit_outcome 7                   |
  +----------------------------------+--------------------------------------------------+
  | program outcome 1                | unit_outcome 2, unit_outcome 4                   |
  +----------------------------------+--------------------------------------------------+
  | program outcome 2                | unit_outcome 1, unit_outcome 3, unit_outcome 6   |
  +----------------------------------+--------------------------------------------------+
  </code></pre>
  
  <p>That in itself is OK, but I want to add in the currently blank program_outcome cell a string like "No matching program outcome."  I would like to do that in the PHP in my code, but not sure how to handle that:</p>
  
  <pre><code>$query = "SELECT MAX(unit.unit_pk) AS unit_pk,
         GROUP_CONCAT(CONCAT('&lt;strong&gt;',unit.unit_code,': &lt;/strong&gt;', unit_outcome.unit_outcome) SEPARATOR '|') unit_outcomes,
         MAX(program_outcome.program_outcome) program_outcome,
         GROUP_CONCAT(unit_outcome.unit_outcome_pk) unit_outcome_pks, 
         program_outcome.program_outcome_pk,
         program_outcome.program_outcome
  FROM unit
  LEFT JOIN unit_unit_outcome_lookup
      ON unit_unit_outcome_lookup.unit_fk = unit.unit_pk
  LEFT JOIN unit_outcome
      ON unit_outcome.unit_outcome_pk = unit_unit_outcome_lookup.unit_outcome_fk
  LEFT JOIN program_outcome_unit_outcome_lookup
      ON program_outcome_unit_outcome_lookup.unit_outcome_fk = unit_outcome.unit_outcome_pk
  LEFT JOIN program_outcome
      ON program_outcome.program_outcome_pk = program_outcome_unit_outcome_lookup.program_outcome_fk
  GROUP BY program_outcome_pk ORDER BY cast(program_outcome as unsigned) ASC";
  
  $result = $connection-&gt;query( $query );
  
  echo "&lt;table width='100%' border='1'&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Program Outcomes&lt;/th&gt;&lt;th&gt;Unit Outcomes&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;";
  while ($row = mysqli_fetch_array($result)) {
  echo "&lt;tr&gt;&lt;td&gt;" . $row['program_outcome'] . "&lt;/td&gt;&lt;td&gt;" . str_replace('|', '&lt;p&gt;',$row['unit_outcomes']) . "&lt;/td&gt;&lt;/tr&gt;";
  }
  echo "&lt;tbody&gt;&lt;/table&gt;";
  </code></pre>