2011-08-15 68 views
0

我有一个CodeIgniter网站,我正在为零售商制作一些统计数据。我需要找到产品'category1'等于1的所有销售产品的product_cost总和。查找销售商品的产品成本总和

下面的数据结构显示'products'和'order_items',顾名思义它包含所有项目出售。

products 
--- 
product_id 
category1 
product_price 
product_cost 

order_items 
--- 
order_id 
product_id 
quantity 

所以基本上我试图让笨正确的SQL语句,形成我orders_model的功能。

感谢您的帮助。 :)

回答

0

不使用活​​动记录,只是想一个简单的查询假设,这应该给你结果你需要:

SELECT SUM(product_cost) AS total_cost FROM products RIGHT JOIN order_items ON products.product_id = order_items.product_id WHERE products.category1 = 1 

活动记录会是这个样子:

$this->db->select('SUM(product_cost) AS total_cost'); 
$this->db->from('products'); 
$this->db->join('order_items', 'products.product_id = order_items.product_id', 'right'); 
$this->db->where('products.category1',1); 
$query = $this->db->get(); 
+0

嗨,非常感谢你的回复。我的确在使用ActiveRecord。试图首先自己写,但它打破了。哈。 – doubleplusgood

+0

哈哈没问题。看到更新的代码:) – AlienWebguy

+0

完美。谢啦。 – doubleplusgood