mysql中只返回不同?

时间:2010-11-19 09:59:57

标签: mysql

select sum(price) 
from product 
where id in (select productid 
             from orders where status!=0 
             and userid=1)

如果我的返回productid =(1,2,2)

总和只是1和2之和,而不是1,2和2的总和

我试过

select sum(price) from product where id in (1,2,2)

同样的结果,我怎么得到模糊的总和?

1 个答案:

答案 0 :(得分:4)

SELECT SUM(p.price)
FROM product AS p
    LEFT JOIN orders AS o
        ON p.id = o.productid
WHERE o.productid IS NOT NULL
    AND o.status <> 0
    AND o.userid = 1