+-----+-----+-------+
| 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();
Now the above code will return as many rows as there is price,
I want to return only one row where there is total of qty and the last value of price
Expected output:
Array (
[0] => stdClass Object
(
[total] => 19
[last_price] => 300
)
)
I have a table with thousands of rows of qty and price, but I want to select the total sum of column qty and only the last value of the price, not sum of qty and all rows of price, I want only the last value of price how do I write my select query ?
Thank you in advance.
$this->db->select("SUM(qty) as total, price as last price ORDER BY PID DESC limit 1");