title: mysql switch case
body: <p>I have a query structure like below, Im wondering if there is a way to the write the select queries as one using  CASE statements or by some other means so that the values get inserted into the appropriate variables based on their values.</p>
  
  <pre><code>DECLARE passes INT;
  DECLARE fails INT;
  ..
  
  SELECT count(score)
  INTO passes
    FROM scores
   WHERE score &gt;= 40;
  
  SELECT count(score)
  INTO fails
    FROM scores
   WHERE score &lt; 40;
  </code></pre>
  
  <hr>
  
  <p>Murdoch came up with a neat solution to this problem, I just had to make one change to it  to put each of values in to there respective variables</p>
  
  <pre><code>SELECT * 
  INTO   passes, fails 
  FROM  (SELECT SUM(CASE 
                      WHEN score &gt;= 40 THEN 1 
                      ELSE 0 
                    END) AS _passes, 
                SUM(CASE 
                      WHEN score &lt; 40 THEN 1 
                      ELSE 0 
                    END) AS _fails 
         FROM   scores) AS x; 
  </code></pre>