2016-07-31 42 views
0

正如您可以通过标题看到的,我无法真正说出我想要做的事情。基本上我有2个表:SQL将子查询与其他结果相乘

--offers-- 
offer_id varchar 
offer_price int 

--carts-- 
user_name varchar 
offer_id varchar 
offer_count int 

现在我想在购物篮(车)的所有项目的总价格。我来了这么远:

select sum(offer_price) from offers where offer_id IN 
(
    select offer_id from carts where user_name='root' 
); 

但是,这种方法完全忽略了购物篮的offer_count值。我将如何实现? (我是比较雏在SQL)

回答

1

你应该加入表:

select 
    sum(c.offer_count * o.offer_price) total 
from carts c 
    join offers o 
on c.offer_id = c.offer_id 
where c.user_name = 'root' 
+0

让他明白你在做什么,在这里你可以加一点解释。 – antorqs

+0

非常感谢!尽管你犯了一个错字。它应该是“在c.offer_id = o.offer_id”,只要有人有相同的问题;-) – Addi