我有2张桌子。到期贷款和付款。付款表中包含due_loan_id。现在,我想查找属于到期贷款的所有付款,并在下面的SQL中执行计算。 此sql查询在MySQL工作台中运行良好。但是,我无法在Rails中工作。我不知道如何在find_by_sql中传递变量和组。
select d.id dueid, d.issue_loan_id, d.duedate,d.dueamount, sum(ifnull(amount, 0))paid, dueamount - sum(ifnull(amount, 0))balance from due_loans d
left join payments p
on d.id = p.due_loan_id
where d.issue_loan_id = 15
group by dueid
如果没有相应的到期贷款付款,那么我将计算分配为0。在其他解决方案中,这就是我尝试过的方法。
def get_due_loans_results
@dueloans = DueLoan.find_by_sql(["select d.id dueid, d.issue_loan_id, d.duedate, d.dueamount, sum(ifnull(amount, 0))paid, dueamount - sum(ifnull(amount, 0))balance from due_loans d
left join payments p
on d.id = p.due_loan_id
where d.issue_loan_id =?", params[:issue_loan_id]].group_by => 'dueid')
respond_to do |format|
format.json {render json: @dueloans}
end
end
答案 0 :(得分:1)
您尝试过吗:
with open("file.txt", "w", encoding="utf-8") as file
file.write("your_text")