ubm ubm - 1 month ago 10
MySQL Question

Averge calculation codeigniter query

Iam trying to find average of rate,but below codeignier query results error "Unknown column 'prate' in 'field list'"

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


How can i solve this

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