1

I need to get all values from table product , EXCEPT they exits in 2 other table products. Is this query acceptable using 2 EXCEPTS ? Should this be done differently .

CREATE table missing_values
AS
select post
from product
EXCEPT
select post
from product_table_a
EXCEPT
select post
from product_table_b
;
MatBailie
  • 83,401
  • 18
  • 103
  • 137
Jorge Vidinha
  • 404
  • 7
  • 20
  • 1
    Your question is unclear. If a value exists in `product` and only ONE of the other two tables, should it be excluded by your query ? Or should it only be excluded if it exists in BOTH of the other two tables? Also ***have you actually tried it yourself***? – MatBailie Sep 07 '15 at 16:22
  • @JuanCarlosOropeza - Please don't make edits that add no material benefit. – MatBailie Sep 07 '15 at 16:26
  • @ MatBailie - If there is a value in product table not found in product_table_a or product_table_b it should be added to the missing_values table being created , this is it. – Jorge Vidinha Sep 07 '15 at 16:30
  • x-posted to http://dba.stackexchange.com/q/114370/7788 – Craig Ringer Sep 07 '15 at 23:20
  • @Craig Ringer . Right first posted at dba , there was no audience . Than posted here . Thanks for noticing – Jorge Vidinha Sep 07 '15 at 23:26
  • @JorgeVidinha No probs. The cross links just help people find it and reduce time wasted by asking duplicate followup questions. It's nice if you include cross links in the question if you're going to post multiple places. BTW, remember timezones and people's work. 8 hours isn't "no audience", it's just "not answered instantly" – Craig Ringer Sep 07 '15 at 23:28
  • @CraigRinger good to know , thanks. Btw any advice on the question ? – Jorge Vidinha Sep 07 '15 at 23:31
  • @JorgeVidinha Existing answers are fine. – Craig Ringer Sep 07 '15 at 23:36
  • Related answer with basic techniques: [Select rows which are not present in other table](http://stackoverflow.com/a/19364694/939860) – Erwin Brandstetter Sep 08 '15 at 02:55

2 Answers2

2

As MatBailie say, you should try your query and see if that give you the result you want.

But in case that doesnt work you can solve it with a double not exists

This mean p1 isn't found neither in p2 or p3.

SELECT post
FROM product p1
WHERE not exists (SELECT p2.post                       
                  FROM product_table_a p2
                  WHERE p1.post = p2.post)
AND   not exists (SELECT p3.post                       
                  FROM product_table_b p3
                  WHERE p1.post = p3.post)

Also this is a probably a more eficient way

SELECT post
FROM product p1
left join product_table_a p2
   on p1.post = p2.post
left join product_table_b p3
   on p1.post = p3.post
WHERE p2.post is null
and   p3.post is null
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
1

A - B - C is the same as A - (B + c):

SELECT post
FROM product
EXCEPT
(
  SELECT post
  FROM product_table_a
  UNION ALL
  SELECT post
  FROM product_table_b
) AS sum;
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275