2013-02-12 73 views
0
products (products_id, name, type) 
orderform (orderform_id, product_id, country_id, price) 

我想要獲取某個country_id的orderform表中不存在的所有產品。SQL留下了額外的where子句

以下是不正確的,我知道有一個更快的方法,速度不是真的只是得到正確的結果集的問題。

SELECT * FROM products p 
    left JOIN orderform o ON o.product_id = p.product_id 

WHERe o.product_id is NULL AND o.jur_id = 10 

感謝球員和加爾斯

回答

0
SELECT * 
FROM products p 
WHERE product_id NOT IN(SELECT product_id 
         FROM orderform 
         WHERE jur_id = 10); 

也嘗試移動額外的條件,連接條件:

SELECT * 
FROM products p 
left JOIN orderform o ON o.product_id = p.product_id 
         AND o.jur_id = 10       
WHERe o.product_id is NULL; 
+0

是的,只需要將它添加到連接條件,我到了那裏ventically。謝謝 – Tristanisginger 2013-02-12 12:32:18

0

這將是一個簡單的

SELECT * 
FROM products p 
WHERE products_id NOT IN (SELECT DISTINCT product_id 
          FROM orderform 
          WHERE country_id = 10) 

我不能看到jur_id在你的模式中,如果你想要你也可以應用那個過濾器

+0

oops jur_id是country_id – Tristanisginger 2013-02-12 12:31:15

0

它會看起來像這樣! (雖然不是最好的辦法)

SELECT * FROM products p 
left JOIN (select * from orderform WHERe product_id is NULL AND jur_id = 10) o ON o.product_id = p.product_id