gpt4 book ai didi

mysql - 如何在 Codeigniter 中编写此子查询 Select AVG with Limit

转载 作者:行者123 更新时间:2023-11-29 17:01:05 25 4
gpt4 key购买 nike

SELECT AVG(harian) a from (select harian from sla limit 2) b

以上是在 Heidisql 中执行的工作,

刚刚尝试过以下:

$rata2 = $this->db->query('SELECT AVG(harian) a from (select harian from sla limit 2) b');

但是,似乎没有输出,请帮忙,谢谢

最佳答案

// Sub Query
$this->db->select('harian')->from('sla')->limit(2);
$subQuery = $this->db->get_compiled_select();

// Main Query
$rata2 = $this->db->select('AVG(harian)')
->from($subQuery)
->get();

return $rata2;

或者如果你想返回数组中的数据

// Sub Query
$this->db->select('harian')->from('sla')->limit(2);
$subQuery = $this->db->get_compiled_select();

// Main Query
$rata2 = $this->db->select('AVG(harian)')
->from($subQuery)
->get()
->toArray();

return $rata2;

关于mysql - 如何在 Codeigniter 中编写此子查询 Select AVG with Limit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52257802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com