title: Overwrite/Join MySQL table with another table on select
body: <p>I have two product tables. Table A is the "default" table (comes from data feed is automatically updated weekly). Table B is used to manually overwrite the values in Table A and has the same columns as Table plus a few more. Both tables always have the same UniqueID column. </p>
  
  <p>I need a select statement that pulls all the values from A and all the values from B, with B overwriting the values from A UNLESS the value from B is null, = to 0 or equal to 0.00 (column set to integer with 2 decimal points and been filled with "0"). </p>
  
  <p>I tried using a LEFT JOIN but am having trouble with the null and "0" values. </p>
  
  <p>I am using the data in php and can deal with it when I only need to pull in 1 row. I query for each, extract the assoc array for A, filter the B array and then extract it. But that process becomes very clunky when start to pull in 20-200 rows. </p>
  
  <p>Table A has about 400,000 rows and can't see B getting over 10,000-20,000. </p>
  
  <p>Thanks for your help. </p>
  
  <p>UPDATE: Here is the code I got to work.</p>
  
  <p><code>SELECT a.UniqueId,
          IF(b.Color, b.Color, a.Color) as Color,
      IF(b.Price1 = 0, a.Price1, b.Price1) as Price1, 
      b.SPrice1
      FROM productdata a LEFT JOIN product_overrides b ON a.UniqueID = b.UniqueID  WHERE a.UniqueID =  $prod";
       </code></p>
  
  <p>UPDATE 2: So simple is better. IF() addresses both null and zero.</p>
  
  <p>IF(expr1,expr2,expr3)</p>
  
  <p>If expr1 is TRUE (<strong>expr1 &lt;> 0 and expr1 &lt;> NULL</strong>) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used. </p>
  
  <p><code>SELECT a.UniqueId,
          IF(b.Color, b.Color, a.Color) as Color,
      IF(b.Price1, b.Price1, a.Price1) as Price1, 
      b.SPrice1
      FROM productdata a LEFT JOIN product_overrides b ON a.UniqueID = b.UniqueID  WHERE a.UniqueID =  $prod";
       </code></p>