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

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

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

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

谢谢男生女生

4

3 回答 3

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;
于 2013-02-12T11:54:45.750 回答
0

这个很简单

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

我在您的架构中看不到jur_id,如果您愿意,您也可以应用该过滤器

于 2013-02-12T11:55:02.927 回答
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
于 2013-02-12T11:58:57.730 回答