我是codeigniter的初学者 我有这样的查询,我想在codeigniter中使用此查询
SELECT sum(price)
FROM (SELECT price
FROM items
ORDER BY price DESC
LIMIT 3
) AS subquery;
我做过
$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();
这会产生这样的输出
SELECT sum(price), price
FROM items
ORDER BY price DESC
LIMIT 3;
我该怎么办?
答案 0 :(得分:6)
像这样使用
$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();
答案 1 :(得分:0)
如果您的查询工作正常,请将其简化。
$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());
答案 2 :(得分:0)
您可以使用这样的查询
$this->db->select_sum('price');
$this->db->from('items');
$this->db->order_by('price desc');
$this->db->limit(3);
$this->db->get();
如果要将数据放入数组,则:
$data=$this->db
->select_sum('price')
->from('items')
->order_by('price desc')
->limit(3)
->get();
return $data->result_array();
答案 3 :(得分:-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;
}
}
答案 4 :(得分:-2)
尝试此操作来修复它:
/**
* [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
}