产品客户名单没有订购?

时间:2011-10-15 20:04:03

标签: mysql

我在这里遇到了一个问题。我需要看看客户X没有购买的产品。即,哪些产品'Jane D'(客户)尚未订购。以下是数据库设计。虽然编码我最终得到了这个客户订购的产品,但无法获得他/她没有订购的产品。你能帮忙吗?

我想要的是库存中的产品列表,而客户尚未订购。不是产品表中的那个

Customer: customer_id, customer_name

Order: order_id, customer_id,inventory_id, order_Date

Inventory: Inventory_id, product_ID

Product: product_id, product_name

from below query i can find what customer_id=1 has bought


SELECT p.Product_ID, p.Product_NAME
FROM CUSTOMER C JOIN Order O ON c.customer_id=o.customer_id
JOIN INVENTORY I ON o.inventory_id=i.inventory_id
JOIN product p ON i.product_id=p.product_id
WHERE C.CUSTOMER_ID='1';

2 个答案:

答案 0 :(得分:2)

SELECT p.Product_ID, p.Product_NAME
FROM product p 
JOIN INVENTORY i ON p.product_id = i.product_id
LEFT JOIN Order o
        ON o.inventory_id = i.inventory_id
        AND o.customer_id = '1'
WHERE o.customer_id IS NULL

答案 1 :(得分:0)

以下查询将提供所需的结果。右外连接和空检查可以帮到你。

SELECT p.Product_ID, p.Product_NAME
FROM CUSTOMER C JOIN Order O ON c.customer_id=o.customer_id
JOIN INVENTORY I ON o.inventory_id=i.inventory_id
RIGHT OUTER JOIN product p ON i.product_id_id=p.product_id
WHERE C.CUSTOMER_ID='1' and i.product_id is null;