2010-11-19 56 views
1
select sum(price) 
from product 
where id in (select productid 
      from orders where status!=0 
      and userid=1) 

如果我的返回的productid =(1,2,2)mysql中只返回不同的?

總和將只能是1之和2代替1,2-總和,AND2

我試圖

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

同樣的結果,我如何得到模糊的總和?

回答

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 
+0

這是真棒,謝謝 – 2010-11-19 10:09:01

+0

你可能想驗證答案,如果它太棒了 – Kennethvr 2010-11-19 10:10:17