Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select SUM from Result with Limit in Codeigniter

I am the beginner of codeigniter.
I have a query like this, I want to use this query in codeigniter

SELECT sum(price) 
FROM (SELECT price
      FROM items
      ORDER BY price DESC
      LIMIT 3
) AS subquery;

I have did

$this->db->select('SUM(price)');
$this->db->select('price');
$this->db->from('items');
$this->db->order_by('price desc');
$this->db->limit(3);
$this->db->get();

This gives output like this

SELECT sum(price), price
      FROM items
      ORDER BY price DESC
      LIMIT 3;

What Can I do?

like image 965
Praveen Srinivasan Avatar asked Apr 02 '15 09:04

Praveen Srinivasan


1 Answers

Use like this

$this->db->select_sum('price');
$this->db->select('price');
$this->db->from('items');
$this->db->order_by('price desc');
$this->db->limit(3);
$this->db->get();
like image 130
Saty Avatar answered Sep 20 '22 19:09

Saty