php 在 Codeigniter 中从 Result with Limit 中选择 SUM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29409776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:23:49  来源:igfitidea点击:

Select SUM from Result with Limit in Codeigniter

phpmysqlcodeignitersum

提问by Praveen Srinivasan

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

我是codeigniter的初学者。
我有一个这样的查询,我想在 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?

我能做什么?

回答by Saty

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

回答by Praveen Srinivasan

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

回答by Raja

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

回答by Suraj Vaghela

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

回答by Maicol Romero

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




 }