在我的代码点火器项目中,用户使用下拉方式输入课程代码和区域中心。根据这两项数据,系统应计算相关课程代码和区域中心过去3年的总收入。
例如,在“”表中,课程代码-ABC1123和Perth区域中心有4个数据。系统应该为“收入总和”一栏找到最新的3年收入。课程代码和区域中心由用户选择。
请您告诉我上述语句的SQL查询是否使用CodeIgniter。

发布于 2022-09-25 10:50:24
select sum(income)
from budget
where code = ? and center = ?
and year > (
select max(year) - 3
from budget
where code = ? and center = ?
)内部查询获取代码和中心的最新年份,然后减去3,然后外部查询将代码和中心在大于计算年份的年份中的所有收入进行汇总,因此在过去3年中是如此。
对于CodeIgniter3,这将是:
$this->db->query(
'select sum(income)
from budget
where code = ? and center = ?
and year > (
select max(year) - 3
from budget
where code = ? and center = ?
)', array($code, $center, $code, $center)); // ->row() or ->row_array()对于CodeIgniter4,使用$db->query(...)而不是$this->db->query(...)。
https://stackoverflow.com/questions/73843004
复制相似问题