如何从mysql中两次select的结果中减去 [英] How to substract from the resut of two selects in mysql

查看:34
本文介绍了如何从mysql中两次select的结果中减去的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想减去两个数,一个是某个字段的内容,一个是查询结果的总和,我该怎么做?这显示了我想要的,但显然不起作用,怎么办?

I want to substract two numbers, one is the contents of a field, the other is the sum of a query result, how can I do it? this shows what I want, but obviously does not work, how to do it?

SELECT monto FROM programaPago WHERE id=2
 **MINUS**
SELECT SUM( caja.monto ) FROM caja  WHERE caja.programaPago_id =2

推荐答案

试试这个:

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

这篇关于如何从mysql中两次select的结果中减去的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆