title: How does MySQL CONCAT IFNULL work with more than two values?
body: <p>I need to <code>CONCAT</code> two fields, if they both exist and add parentheses around the second field.  Otherwise I just need to list the first field.</p>
  
  <p>Here's the simple version:</p>
  
  <pre><code>SELECT id, CONCAT(name,' (',nickname,')') as name FROM user;
  </code></pre>
  
  <p>As long as name and nickname both exist you should get results like:</p>
  
  <pre><code>1 | Adam (Alpha Dog)
  2 | Bob (Bobby)
  3 | Charles (Charlie)
  </code></pre>
  
  <p>But if there is no nickname it just skips the whole thing as <code>null</code>.  For example: id <code>4</code>, name <code>Doug</code>, nickname <code>null</code> gives the result:</p>
  
  <pre><code>4 | null
  </code></pre>
  
  <p>What I'd like to see is it to list just the name... like this:</p>
  
  <pre><code>4 | Doug
  </code></pre>
  
  <p>So I started looking at <code>CONCAT</code> <code>IFNULL</code>.  But I'm having a hard time getting it right.  Can you help?</p>
  
  <p>Here are a few examples of things I've tried:</p>
  
  <pre><code>SELECT id, CONCAT(IFNULL(name, ' (', nickname, ')', name) as name FROM user;
  SELECT id, CONCAT(name, IFNULL(' (', nickname, ')')) as name FROM user;
  </code></pre>