codeigniter项目的SQL查询[已关闭]

slhcrj9b  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(116)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
两个月前关门了。
Improve this question
在我的codeigniter项目中,用户通过唐斯输入课程代码和区域中心。根据这两个数据,系统应该计算出相关课程代码和区域中心最近3年的总收入。
例如,在“预算”表中,科目代码-ABC 1123珀斯区域中心有4个数据,系统应该在“收入合计”列中找到最近3年的收入,科目代码和区域中心由用户选择。
您能告诉我使用CodeIgniter对上述语句进行SQL查询吗?

kyks70gy

kyks70gy1#

select sum(income)
from budget
where code = ? and center = ?
and year > (
    select max(year) - 3
    from budget
    where code = ? and center = ?
)

内部查询获取code & center的最近年份,然后减去3。外部查询然后将code & center在大于此计算年份的年份(即过去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(...)

相关问题