查找用户购买但未评级MySQL,加入查询的产品说明

时间:2015-03-08 10:54:15

标签: mysql

我正在尝试查找用户已购买但未评级的产品的说明,但我获得了描述的NULL值,我有以下表格。

客户

id  | customer
----|---------
1   | john
2   | jenkins
3   | jane
4   | janet

产品

id  | description
----|---------
1   | deoderant
2   | soap
3   | shampoo
4   | razor
5   | sponge

订单

customer_id  | product_id
-------------|---------
1            | 1
1            | 2
2            | 3
2            | 4
3            | 5

customer_id  | product_id | rate
-------------|------------|-------
1            | 1          | 1
2            | 2          | 3
2            | 4          | 3
4            | 2          | 2

这是SQL查询。

select description from rate
left join orders on
rate.customer_id = orders.customer_id and rate.product_id = orders.product_id
left join product on
orders.customer_id and product.id
where orders.customer_id is null; 

我得到的结果是:

description   
-------------
null
null              

我认为我正朝着正确的方向前进,为什么我会得到null或/并且这样做是错误的?

1 个答案:

答案 0 :(得分:1)

我想这可行:

select 
  product.description 
from 
  orders
  inner join product on 
    product.product_id  = orders.product_id
  inner join customer on 
    customer.customer_id = orders.customer_id
  left join rate on 
    rate.customer_id = orders.customer_id and 
    rate.product_id  = orders.product_id
where
    rate.rate is null