class Product < ActiveRecord::Base
belongs_to :category
has_many :order_items, dependent: :destroy
end
class OrderItem < ActiveRecord::Base
belongs_to :order
belongs_to :product
end
我需要列出所有产品及其 order_item 的数量总和及其总价格总和
Product
id name
1 product_1
OrderItem
product_id order_id quantity total_price
1 1 10 200
1 2 10 200
for example expecting output should be
name quantity total_price
product_1 20 400
select p.name, sum(o.quantity) as quantity, sum(o.total_price) as total_price
from Product p
join OrderItem o on p.id = o.product_id
group by p.name
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)