【问题标题】:How to multiply two columns and sum it in codeigniter active record?如何将两列相乘并在codeigniter活动记录中求和?
【发布时间】:2020-05-10 18:20:39
【问题描述】:
ITEM  | PRICE | QTY
lamp  | 5     | 3
lamp  | 7     | 2
pen   | 3     | 15
pen   | 5     | 10

我需要查询codeigniter活动记录中所有灯的价格总和。结果应该是 29。

【问题讨论】:

标签: codeigniter activerecord


【解决方案1】:

您需要将column namesas 相乘以存储该数据,然后对该值使用select_sum。我已经写了一个查询,看看它是否对你有帮助。

$query = $this->db->select_sum('(PRICE * QTY)', 'total')->where('ITEM', 'lamp')->from('your-table-name')->get()->result();
/* 
Produces: 
SELECT SUM((PRICE * QTY)) AS `total` FROM `your-table-name` WHERE `ITEM` = 'lamp'
*/

输出

Array
(
    [0] => stdClass Object
        (
            [total] => 29
        )

)

现在得到这个输出 -

$total = $query[0]->total; // 29

【讨论】:

  • 它工作得很好,先生,谢谢...$this->db->select_sum('(PRICE * QTY)', 'total'); $this->db->where('ITEM', 'lamp'); $this->db->from('products'); return $this->db->get()->result();
  • 谢谢你!如果它对您有所帮助,请考虑投票和/或接受作为答案。 :)
  • 完成,并带有此消息“感谢您的反馈!声望低于 15 人的投票将被记录,但不要更改公开显示的帖子得分。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-28
相关资源
最近更新 更多