title: mysql where condition priority
body: <p>I have the following query and it gets the first result "Edit: of each contact" as I want but I do not need any other results per contact after. So example if they have 3 phone # and one is main, it gives me the main then the other 2. I have tried grouping but it ends up getting the first one pulled no matter if main or not. Any thoughts to how I could do this without doing a php check in the loop for if contact already exists?</p>
  
  <pre><code>SELECT pc.firstname, pc.lastname, pp.areacode, pp.prefix, pp.last4
  , if(pc.mainphone = pp.phoneid,1,0) as phone_priority
  FROM contacts pc
  JOIN phone pp ON ( (pp.contid = pc.contid &amp;&amp; pp.phoneid = pc.mainphone) || (pp.contid = pc.contid) )
  ORDER BY pc.lastname ASC, pc.firstname ASC, phone_priority DESC
  </code></pre>
  
  <p>Table setup:</p>
  
  <pre>
  contacts (id, firstname, lastname, mainphone)
  phone (id, areacode, prefix, last4)
  </pre>
  
  <p>Using MySQL 4.</p>