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.
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”).
I tried using a LEFT JOIN but am having trouble with the null and “0” values.
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.
Table A has about 400,000 rows and can’t see B getting over 10,000-20,000.
Thanks for your help.
UPDATE: Here is the code I got to work.
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";
UPDATE 2: So simple is better. IF() addresses both null and zero.
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3. IF() returns a numeric or string value, depending on the context in which it is used.
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";
I think this aught to cover all your scenarios: