5
New! Save questions or answers and organize your favorite content.
Learn more.
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?
asked
Apr 2, 2015 at 9:44
3
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();
answered Apr 2, 2015 at 9:46
SatySaty
22.3k7 gold badges30 silver badges49 bronze badges
3
you can use a query like this
$this->db->select_sum('price');
$this->db->from('items');
$this->db->order_by('price desc');
$this->db->limit(3);
$this->db->get();
If you want to put data into an array then:
$data=$this->db
->select_sum('price')
->from('items')
->order_by('price desc')
->limit(3)
->get();
return $data->result_array();
answered Nov 1, 2018 at
7:50
make it simple if your query is working fine.
$query = $this->db->query('SELECT sum(price) FROM (SELECT price FROM items ORDER BY price DESC LIMIT 3 ) AS subquery');
print_r($query->result_array());
answered Apr 2, 2015 at 9:49
RajaRaja
6005 silver
badges14 bronze badges
1
public function advanceSalary($id) {
if ($id) {
$this->db->select('salaryLaser.*');
//$this->db->select_sum('salaryLaser.credit');
$this->db->select('SUM(salaryLaser.credit) as creditTotal');
$this->db->select('SUM(salaryLaser.debit) as debitTotal');
$this->db->from($this->salaryLaser);
$this->db->where('salaryLaser.employeeId', $id);
$this->db->where('salaryLaser.employeeRole', '1');
$advance = $this->db->get();
if ($advance->num_rows() > 0) {
return $advance->row();
} else {
return FALSE;
}
} else {
return FALSE;
}
}
answered Aug 9, 2017 at 11:53
Try this to fix it:
/**
* [total_currency description]
* @param [type] $column_name [description]
* @param [type] $where [description]
* @param [type] $table_name [description]
* @return [type] [description]
*/
function total_count($column_name, $where, $table_name)
{
$this->db2->select_sum($column_name);
// If Where is not NULL
if(!empty($where) && count($where) > 0 )
{
$this->db2->where($where);
}
$this->db2->from($table_name);
// Return Count Column
return $this->db2->get()->row($column_name);//table_name array sub 0
}
answered Jun 16, 2018 at 7:39
Erianto Simalango
unread,
Aug 28, 2009, 7:56:47 AM8/28/09
to
Sekarang masalahnya, saya sedang membuat file transaksi, dengan tampilan..
No. NmBarang Jumlah Harga Total
1. xxxxx 99
9999 9999
2. xxxxx 99 9999 9999
3. xxxxx 99 9999 9999
Total ???? ????
Mohon bantuannya bagaimana cara menggunakan SUM pada PHP untuk menampilkan harga tersebut.
salam
Eric
--
-----------------------------------------------------------------------------------
Office : PT. SUCOFINDO (Persero) Pekanbaru Branch
-----------------------------------------------------------------------------------
Linux Community; Ubuntu register : #17041
-----------------------------------------------------------------------------------
IBO ID: 92.46.7140 | ERIANTO SIMALANGO
Ph :
+62-761-7076141 | Mp :
+62-819-33703356
http://www.eriantosimalango.co.cc
-----------------------------------------------------------------------------------
--
-----------------------------------------------------------------------------------
Office : PT. SUCOFINDO (Persero) Pekanbaru Branch
-----------------------------------------------------------------------------------
Linux Community; Ubuntu register : #17041
-----------------------------------------------------------------------------------
IBO ID: 92.46.7140 | ERIANTO SIMALANGO
Ph :
+62-761-7076141 | Mp : +62-819-33703356
http://www.eriantosimalango.co.cc
-----------------------------------------------------------------------------------
Aziz Purnama
unread,
Aug 28, 2009, 4:56:55 PM8/28/09
to
u/ MySQL :
$sql = "SELECT SUM(HARGA),SUM(TOTAL) FROM BARANG";
$query = mysql_query($sql);
$result = mysql_fetch_row($query);
$harga = $result[0];
$total = $result[1];
Erianto Simalango
unread,
Sep 1, 2009, 5:49:34 AM9/1/09
to
tapi total itu bukan database...
apakah tetap bisa? karena total dari hasil perkaliah harga dengan jumlah
Aziz Purnama
unread,
Sep 1, 2009, 8:46:09 AM9/1/09
to
Sangat bisa, tinggal pake querynya dikalikan saja :
SELECT SUM(HARGA * JUMLAH) FROM BARANG