在 Codeigniter 中从具有限制的结果中选择 SUM

问题描述 投票:0回答:6

我是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;

我能做什么?

php mysql codeigniter sum
6个回答
10
投票

这样使用

$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();

5
投票

您可以使用这样的查询

$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();

1
投票

如果您的查询工作正常,请使其变得简单。

$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());

0
投票

要实现 CodeIgniter 的活动记录查询构建方法,请准备并编译子查询,然后使用

select_sum()
执行父查询,并将子查询输入到
get()
调用中。

在两个查询构建链中,

from()
方法都可以省略,因为表名可以通过
get_compiled_select()
传入,子查询可以通过
get()
传入。

我已经测试了以下内容,可以在 CI3 应用程序中成功运行。

public function sumTopThreePrices()
{
    $sub = $this->db
        ->select('price')
        ->order_by('price', 'DESC')
        ->limit(3)
        ->get_compiled_select('items');

    return $this->db
        ->select_sum('price')
        ->get("($sub) sub)
        ->row()
        ->price;
}

渲染的 SQL:(引用可能因方言/数据库而异)

SELECT SUM("price") AS "price"
FROM (SELECT "price"
FROM "items"
ORDER BY "price" DESC
 LIMIT 3) sub

-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;
        }
    }

-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




 }
© www.soinside.com 2019 - 2024. All rights reserved.