【发布时间】:2017-04-24 09:26:17
【问题描述】:
+-----+-----+-------+
| PID | Qty | Price |
+-----+-----+-------+
| 1 | 5 | 100 |
| 2 | 10 | 500 |
| 3 | 4 | 300 |
+-----+-----+-------+
$this->db->select("SUM(qty) as total, price as last price");
$this->db->from('table');
$query = $this->db->get();
$result = $query->result();
现在上面的代码将返回与价格一样多的行,
我只想返回一行,其中有总数和价格的最后一个值
预期输出:
Array (
[0] => stdClass Object
(
[total] => 19
[last_price] => 300
)
)
我有一个包含数千行数量和价格的表,但我想选择列数量的总和,并且只选择价格的最后一个值,而不是数量的总和和所有价格行,我只想要价格的最后一个值如何编写我的选择查询?
提前谢谢你。
【问题讨论】:
-
你如何定义最后一行?
-
$this->db->select("SUM(qty) as total, price as last price ORDER BY PID DESC limit 1"); -
@AlivetoDie 以上不起作用..
-
@AlivetoDie 是的,它不起作用
-
@Forward 最后一行是 price 列的最后一个值
标签: php mysql codeigniter