1

I have two tables products and product_edits which hold product information on the pricelist. My app works in a way that if user changes any product info in products table it inserts it into product_edits table...

PRODUCTS table

pk|code|name     |description|price|....
-----------------------------------
1 |QW1X|Product 1|...
2 |LW1X|Product 2|...
3 |DE1X|Product 3|...

PRODUCT_EDITS table

pk|product_id|code|name              |description|price|....
-----------------------------------
1 |         2|LW1X|Product 2 new name|...

In above case I would like an SQL that returns records from both tables, but if product is found in product_edits table it selects only from product_edits and not also from products table.

I tried using standrd union but selects all records from both tables:

select code, name, description from products
union
select code, name, description from product_edits
1
  • Are you keeping multiple product edits in that table - i.e. if you update LW1X to "product 2 new-new name" will you then have two records in product_edits, or just the one? If the latter, I would ask what the point would be (why not just update products?), if the former, you will need to deal with your subselection. Mar 9, 2015 at 16:39

3 Answers 3

2

It's better to use EXISTS instead of IN, in this case. You want the search to stop once you found a match, not go over all of the results from product_edits.

So do it like this:

SELECT
    code, name, description
FROM products p
WHERE NOT EXISTS (SELECT 1 FROM product_edits e WHERE e.code = p.code)
UNION
SELECT
    code, name, description
FROM product_edits
1
  • yes this is the preferred behaviour and optimised... though the solution provided by @scubasteve623 is also working Mar 9, 2015 at 18:41
1
select code, name, description from products
where code not in(select code from product_edits)
union
select code, name, description from product_edits
-1

You may use IFNULL function http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_ifnull

If your tables are related, you can try something like this:

SELECT 
    p.code, IFNULL(pe.name, p.name), IFNULL(pe.description, p.description) 
from 
    products p 
    left join product_edit pe on (p.id = pe.product_id)
UNION
SELECT 
    pe2.code, pe2.name, pe2.description
from 
    product_edits pe2

The first part will give you the products that are only in products table and the products that are in both tables, but with product_edits.description.

The second part will give you the products that are only in products_edits table, because union will remove repeated records

3
  • This dos not work, still selects rows from both tables Mar 9, 2015 at 18:40
  • Ok, I made a mistake. The colunm name that varies. So, IFNULL should be applied in that column, not description (or both columns if description and name are different). Mar 9, 2015 at 19:33
  • The important here is the concept of to return the equal record in two parts of union. I'm sorry for the mistake. Mar 9, 2015 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.