title: Mysql query works unexpectedly: after an if() a different value is selected in a calculation; other value is NULL
body: <p>I am confused why my query gives the desired result while I think it should not. I am selecting orders from one table, joining this with the package belonging to this order (which exists for some but not all of the orders). If there is no package for this order I want to select the product length and width instead. Afterwards I make a calculation on how expensive this will be to send. There I use the value from the packages table, which is NULL for some of the orders. Yet it does return the values as if it is calculating with the values from the productvariations table. So in this result I get packages_id = NULL and the packageCost is calculated with the values from the productvarations table.</p>
  
  <pre><code>select p.packages_id,
        if(p.package_length is null,pv.package_length,p.package_length) as package_length, 
        if(p.package_width is null, pv.package_width, p.package_width) as package_width, 
        if(p.package_length+2*p.package_width+2*p.package_height&lt;3000,5.00,9.55) as packageCost
  from orders o
  left join packages p on o.package_id = p.package_id
  left join productvariations pv on o.productvariation = pv.productvariation;
  </code></pre>
  
  <p>The question is: Is mysql taking the values for package_length and width from productvariations automatically if the values in packages are NULL?</p>