title: MySQL: How do you count non empty fields in a row, then count the same values?
body: <p>Keep in mind that there's no way I can normalize the table, so I'm having to work with what I got. In the table, the rows are similar to this</p>
  
  <pre><code>name  |  widget1  |  widget2 | widget3 
  ------+-----------+----------+----------
  Joe   |   blue    |   red    |          
  Jane  |   green   |          |          
  Bob   |   red     |   red    |  green
  Susy  |   green   |  green   |   
  </code></pre>
  
  <p>What I'd like to do is count the total number of widgets (Joe has 2 widget, Jane has 1, etc), and also count the number of similar widgets (Bob has 3 widgets - 2 red and 1 green, Susy has 2 widgets- 2 green, etc)</p>
  
  <p>Here's my code to count the total number of widgets:</p>
  
  <pre><code>SELECT ( 
           SUM( IF(widget1 &lt;&gt; "", 1, 0) ) +
           SUM( IF(widget2 &lt;&gt; "", 1, 0) ) +
           SUM( IF(widget3 &lt;&gt; "", 1, 0) )
         ) AS totalWidgets FROM table
  </code></pre>
  
  <p>Which works fine. But is there a better way to do this?  Also, to count the number of similar of values, I'm sure I can so something similar but just check if the values are equal... but it could get pretty long and convoluted. </p>
  
  <p>Is there a more direct approach?</p>