我试图在两个日期之间的E1_CHECK为真时对行进行求和。尽管我最好解释这里是我的代码
select e.e1_fecha2,a.a1_nombre,a.A1_NUMCUENTA,f.CF_KIT,e.E1_CC,e.E1_MONTo,f.CF_NUM,f.CF_MONTO
from
e1 as e inner join cf as f on e.CF_num=f.CF_num
inner join a1 as a on f.a1_ID=a.a1_id
where
e.E1_CHECK=true and E1_FECHA2 between '2016-02-01' and '2016-02-29'
它显示了这个
e1_fecha2 | a1_nombre | A1_NUMCUENTA | CF_KIT | E1_CC | E1_Monto | CF_NUM |CF_Monto
-------------------------------------------------------------------------------------------
2016-02-29|khaldun |123456 | 55 |1/5 |1320000 |001-001-0000002|6600000
2016-02-29|khaldun |123456 |8 cameras |1/5 |1100000 |001-001-0000003|5500000
2016-02-29|khaldun |123456 |8 cameras |2/5 |1100000 |001-001-0000003|5500000
2016-02-29|oiuiouio |64 | fff |1/1 |1375000 |001-001-0000004|1375000
2016-02-29|khaldun |123456 | 44444 |1/1 |5500000 |444-444-4422222|5500000
2016-02-29|khaldun |123456 |rrrr |1/1 |1125000 |001-001-0000006|1125000
和E1_Monto
是付款的一部分,CF_MONTO
是我想要的总账单减去CF_Monto - E1_MONTO
以及需要支付多少费用
这就是我想要的样子
e1_fecha2 | a1_nombre | A1_NUMCUENTA | CF_KIT | E1_CC | E1_Monto | CF_NUM | CF_Monto | Payed | Left_to_Pay
---------------------------------------------------------------------------------------------------------------------------
2016-02-29|khaldun |123456 | 55 |1/5 |1320000 |001-001-0000002| 6600000 |1320000 |5280000
2016-02-29|khaldun |123456 |8 cameras |1/5 |1100000 |001-001-0000003| 5500000 |1100000 |4400000
2016-02-29|khaldun |123456 |8 cameras |2/5 |1100000 |001-001-0000003| 5500000 |2200000 |3300000
2016-02-29|oiuiouio |64 | fff |1/1 |1375000 |001-001-0000004| 1375000 |1375000 |0
2016-02-29|khaldun |123456 | 44444 |1/1 |5500000 |444-444-4422222| 5500000 |5500000 |0
2016-02-29|khaldun |123456 |rrrr |1/1 |1125000 |001-001-0000006| 1125000 |1125000 |0
我尝试了其他方法,但它只显示了1行并且总结了所有东西。
答案 0 :(得分:1)
只需选择列并执行操作(减去)。我改变了
SELECT *, E1_MONTO AS payed,CF_Monto - E1_MONTO AS left_to_pay
这一行。希望它有所帮助。
SELECT *, E1_MONTO AS payed,CF_Monto - E1_MONTO AS left_to_pay
FROM e1 AS e INNER JOIN cf AS f ON e.CF_num=f.CF_num
INNER JOIN a1 AS a ON f.a1_ID=a.a1_id
WHERE
e.E1_CHECK=true AND E1_FECHA2 BETWEEN '2016-02-01' AND '2016-02-29'
答案 1 :(得分:0)
我想出了怎么做这就是我做的事情
select
e.e1_fecha2,e.e1_check, a.a1_nombre,a.A1_NUMCUENTA,f.CF_KIT,e.E1_CC,e.E1_MONTo,
(select sum(IF(t.E1_CHECK = true, t.e1_monto , 0)) from e1 as t where e.CF_NUM=t.cf_num ) as payed,
CF_Monto - (select sum(IF(t.E1_CHECK = true, t.e1_monto , 0)) from e1 as t where e.CF_NUM=t.cf_num )AS left_to_pay
from e1 as e
inner join cf as f on e.CF_num=f.CF_num
inner join a1 as a on f.a1_ID=a.a1_id
where
e.E1_CHECK=true and E1_FECHA2 between '2016-02-01' and '2016-02-29'
感谢大家的帮助:)