title: If column value is null, use other column
body: <p>First of all, don't scold me for trying to do this - my boss wants me to dump a list of data from across many tables into a single CSV file.</p>
  
  <p>This CSV contains two categories of people combined into a single sheet: <em>permanent staff</em>, and <em>contractors</em>.</p>
  
  <p>Some values are obtained differently depending on which category you belong to. For example, if you are a <em>contractor</em>, your job description is in <code>contractors.blended_role</code>. If you are <em>permanent</em>, the job description is in <code>roles.blended_role</code> where <code>roles.id = staff.role</code>.</p>
  
  <p>So I tried (something like) this:</p>
  
  <pre><code>SELECT
      stuff,
      (CASE
          WHEN c.blended_role IS NULL THEN
              r.blended_role
          ELSE
              c.blended_role
          END) as `Job Description`,
      morestuff
  FROM
      everyone e
  LEFT JOIN contractors c
      ON c.id = e.id
  LEFT JOIN staff s
      ON s.id = e.id
  LEFT JOIN roles r
      ON r.id = s.role
  </code></pre>
  
  <p>But I got a syntax error</p>
  
  <pre><code>You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'stuff, (CASE WHEN c.blended_role IS NULL THEN r.blended_role EL` at line 2
  </code></pre>
  
  <p>Can anyone suggest an alternative?</p>
  
  <h3>Update</h3>
  
  <p>The syntax error was from a missing comma a couple of lines up.</p>