如何将两列相乘并在 codeigniter 活动记录中求和?
How to multiply two columns and sum it in codeigniter active record?
ITEM | PRICE | QTY
lamp | 5 | 3
lamp | 7 | 2
pen | 3 | 15
pen | 5 | 10
我需要查询 codeigniter 活动记录中所有灯的价格总和。结果应该是 29.
您需要乘以 column names
使用 as
来存储该数据,然后在该值上使用 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'
*/
OUTPUT
Array
(
[0] => stdClass Object
(
[total] => 29
)
)
现在得到这个输出-
$total = $query[0]->total; // 29
ITEM | PRICE | QTY
lamp | 5 | 3
lamp | 7 | 2
pen | 3 | 15
pen | 5 | 10
我需要查询 codeigniter 活动记录中所有灯的价格总和。结果应该是 29.
您需要乘以 column names
使用 as
来存储该数据,然后在该值上使用 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'
*/
OUTPUT
Array ( [0] => stdClass Object ( [total] => 29 ) )
现在得到这个输出-
$total = $query[0]->total; // 29