我想减去两个数字,一个是字段的内容,另一个是查询结果的总和,我该怎么做呢?这显示了我想要的,但显然不起作用,怎么做?
SELECT monto FROM programaPago WHERE id=2
**MINUS**
SELECT SUM( caja.monto ) FROM caja WHERE caja.programaPago_id =2
答案 0 :(得分:2)
你可以做这样的事情
SELECT
(SELECT monto FROM programaPago WHERE id = 2) -
(SELECT SUM(caja.monto) FROM caja WHERE caja.programaPago_id = 2)
但不知道你想要达到的目标可以有更好的方法来实现这个目标
答案 1 :(得分:0)
试试这个:
SELECT ( programaPago.monto - COALESCE(iTotal.TotalMonto,0)) Result
FROM programaPago LEFT JOIN
(SELECT programaPago_id, SUM(monto) as TotalMonto
FROM caja GROUP BY programaPago_id) as iTotal
ON programaPago.ID = iTotal.programaPago_id
WHERE programaPago.ID = 2