Averge计算codeigniter查询

时间:2016-10-11 07:27:51

标签: mysql codeigniter codeigniter-3

Iam试图找到平均费率,但在codeignier查询结果下方错误“字段列表中的未知列'prate'”

 $this->db->select('AVG(prate)as avgprate');
        $this->db->from('stock','(select sum((pdetails.quantity*pdetails.rate)/pdetails.quantity) AS prate FROM `stock` INNER JOIN `pdetails` ON `pdetails`.`stock_id` = `stock`.`stock_id`)');

        $result = $this->db->get();
        $res = $result->row();

我该如何解决这个问题

1 个答案:

答案 0 :(得分:0)

$this->db->select('AVG(t.prate)as avgprate');
        $this->db->from('stock ,(select sum((pdetails.quantity*pdetails.rate)/pdetails.quantity) AS prate FROM `stock` INNER JOIN `pdetails` ON `pdetails`.`stock_id` = `stock`.`stock_id`)t');

        $result = $this->db->get();
        $res = $result->row();