title: MySQL Multiple Concatenation With Different Separators - CONCAT_WS
body: <p>I am dynamically building a MySQL concatenation with multiple fields and different separators.   I'd like to drop the separator if the currently concatenated field is blank or null.  </p>
  
  <pre><code>$concat_string = "CONCAT_WS('".$infix_array[0]."'," . $concat_fields_array[0] . "," . $concat_fields_array[1] . ")";
  $i = 0;
  foreach($concat_fields_array as $concat_field){
      if($i &gt;= 2){
           $concat_string = "CONCAT_WS('".$infix_array[$i-1]."'," . $concat_string . "," . $concat_field . ")";
      }
      $i++;
  }
  </code></pre>
  
  <p>This builds a nested CONCAT_WS like:</p>
  
  <pre><code> CONCAT_WS(', ',CONCAT_WS(' - ',CONCAT_WS(':',CONCAT_WS(' ',field1,field2),field3),field4),field5)
  </code></pre>
  
  <p>Would wrapping the fields in NULLIF like NULLIF(field1, '') get rid of extra infixes?</p>
  
  <p>Is there a faster function to accomplish this?</p>