【问题标题】:MySQL query looping through all the entries in tiMySQL查询循环遍历ti中的所有条目
【发布时间】:2022-01-25 12:52:01
【问题描述】:

我有这个代码:

get_amounts_sum = []
query = """SELECT sp.customer_surname, SUM(sp.amount),
(Select SUM(cp.amount) from customers_payments7777 cp
where cp.customer_AFM = sp.customer_AFM AND sp.date_ <= %s AND cp.date_ <= %s
GROUP BY sp.customer_AFM) amountsum,
sp.monthly, sp.date_ 
FROM set_payment7777 sp
GROUP BY customer_AFM"""

mycursor.execute(query,(to_date,to_date,))
for row in mycursor:
    if row[3].upper() == 'Y':
        num_months = (to_date.year - row[4].year) * 12 + (to_date.month - row[4].month)
        semi_final_amount_Y = (row[1] * num_months) + row[1] 
        final_amount_Y = semi_final_amount_Y - row[2]
        file.write(f"{row[0]}\t{final_amount_Y}\t")
        file.write("\n")
    elif row[3].upper() == 'N':
        final_amount_N = row[1] - row[2]
        file.write(f"{row[0]}\t{final_amount_N}\t")
        file.write("\n")

它从 MySQL 中的两个不同表中获取两个数量。此外,第一个表有一个名为每月的变量。有些金额将该变量设置为 Y(是),有些金额设置为 N(否)。我想取所有设置为 Y 的金额,然后将它们乘以两个日期之间的月份长度,然后从第二个表中的金额中减去它们。如果它没有设置为 Y,我也想做同样的事情,但不要乘以月份。上面的代码工作正常,但问题是它检查是否有一个金额将变量设置为 Y,然后它正在为所有金额执行该过程。我不想要它,我只想对设置为 Y 的金额执行此操作,然后继续处理设置为 N 的金额。

【问题讨论】:

  • 旁白:子查询中不需要GROUP BY sp.customer_AFM,因为它只选择一个customer_AFM

标签: python mysql


【解决方案1】:

如果我正确理解了这个问题,您有两个选择,我相信最简单的一个是对查询结果进行排序,以便首先处理所有“Y”:

"""SELECT sp.customer_surname, SUM(sp.amount),
(Select SUM(cp.amount) from customers_payments7777 cp
where cp.customer_AFM = sp.customer_AFM AND sp.date_ <= %s AND cp.date_ <= %s
GROUP BY sp.customer_AFM) amountsum,
sp.monthly, sp.date_ 
FROM set_payment7777 sp
GROUP BY customer_AFM
ORDER BY sp.monthly DESC"""

或者,您可以有两个查询并分别处理它们:

queryY = """SELECT sp.customer_surname, SUM(sp.amount),
(Select SUM(cp.amount) from customers_payments7777 cp
where cp.customer_AFM = sp.customer_AFM AND sp.date_ <= %s AND cp.date_ <= %s
GROUP BY sp.customer_AFM) amountsum,
sp.monthly, sp.date_ 
FROM set_payment7777 sp
WHERE sp.monthly='Y'
GROUP BY customer_AFM"""

queryN = """SELECT sp.customer_surname, SUM(sp.amount),
(Select SUM(cp.amount) from customers_payments7777 cp
where cp.customer_AFM = sp.customer_AFM AND sp.date_ <= %s AND cp.date_ <= %s
GROUP BY sp.customer_AFM) amountsum,
sp.monthly, sp.date_ 
FROM set_payment7777 sp
WHERE sp.monthly='N'
GROUP BY customer_AFM"""

mycursor.execute(queryY,(to_date,to_date,))
for row in mycursor:
        num_months = (to_date.year - row[4].year) * 12 + (to_date.month - row[4].month)
        semi_final_amount_Y = (row[1] * num_months) + row[1] 
        final_amount_Y = semi_final_amount_Y - row[2]
        file.write(f"{row[0]}\t{final_amount_Y}\t")
        file.write("\n")
        
mycursor.execute(queryN,(to_date,to_date,))
for row in mycursor:        
        final_amount_N = row[1] - row[2]
        file.write(f"{row[0]}\t{final_amount_N}\t")
        file.write("\n")

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 2011-03-09
    • 1970-01-01
    • 2014-10-22
    • 2011-08-14
    相关资源
    最近更新 更多