SQL使用其他where子句保持连接

时间:2013-02-12 11:53:00

标签: mysql left-join

products (products_id, name, type)
orderform (orderform_id, product_id, country_id, price)

我想获取某个country_id的orderform表中不存在的所有产品。

以下是不正确的,我知道有更快的方法,速度不是真正的问题,只是获得正确的结果集。

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

谢谢大家和女士们

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分:0)

这个很简单

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

我无法在您的架构中看到jur_id,如果您还想要,也可以应用该过滤器

答案 2 :(得分: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