title: How do I do this ternary operation in mysql?
body: <pre><code>....
  .....
  if ($prod_price-&gt;RecordCount()&gt;0) {
                $p_price = $prod_price-&gt;fields['options_values_price'];
            }
            else
            {
                $p_price = $orders_products_query-&gt;fields['products_price'];
            }
            $p_price = ($p_price&gt;0)?$p_price:$orders_products_query-&gt;fields['final_price'];
  ......
  ..........
  </code></pre>
  
  <p>As you can guess from the above, I am running two different queries and checking 3 fields to determine the variable $p_price in PHP. I want to condense this in a single query, the conditions being:
  If field1 is Null, use field2, if field2 is 0, use field 3.</p>
  
  <p>The first part can be solved with IFNULL(field1,field2)....but what do I do about the second part? Should I use case? I need the most efficient solution in terms of execution speed, cause this is part of an extremely large query.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Since it seems the question isn't clear to some of you, take this as an alternative.</p>
  
  <p><code>IF(IFNULL(field1,field2)&gt;0,IFNULL(field1,field2),field3)</code>
  The above MySQL query condition works with the above mentioned logic, but as you can see, it evaluates field1 and field2 twice for NULL checking, which I don't believe is very efficient, so I am looking for a better query/condition to rewrite the same thing.</p>