开发者

CodeIgniter ActiveRecord Multiply

开发者 https://www.devze.com 2023-03-10 08:22 出处:网络
I\'m trying to work out the total value of all the products in my inventory. Each product in the table has a price and quantity. So I need to multiply each produc开发者_高级运维t\'s price by the quant

I'm trying to work out the total value of all the products in my inventory. Each product in the table has a price and quantity. So I need to multiply each produc开发者_高级运维t's price by the quantity and then add all of these together to get a total for all products. From a previous question, I now have the MySQL query to do this:

select sum(product_price*product_quantity) as stockvalue from products

But now I'm trying to work out the correct way to do this with CodeIgniter's ActiveRecord.

Grateful for any advice on this. I've looked in the docs but can only find select_sum() for one field, rather than the sum of multiplying two together.

I have tried using the following:

public function getStockValue() {
$this->db->select('sum('product_price'*'product_quantity') as stockvalue', FALSE);
$this->db->from('products');
$query = $this->db->get();
return $query->row()->stockvalue;
}


Try this:

$this->db->select('sum(`product_price`*`product_quantity`) as stockvalue', FAlSE);

I'm not sure if you need the second argument. I haven't worked with CI for quite awhile.


$this->db->select('sum(product_price) * sum(product_quantity) as stockvalue', FALSE);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号