查询多个表中的总和

时间:2018-10-31 19:05:32

标签: mysql sql

你好吗? 我有mysql数据库。 我试图使查询有点复杂。我将写下到目前为止的工作。

查询NO1

SELECT BONOD_AMOUNT, BONOD_DETAILS_ID
FROM  `BONOD_DETAILS` 
WHERE BONOD_ID
IN (
SELECT BONOD_ID
FROM BONOD_TABLE
WHERE USER_ID =16
)

结果是:

    BONOD_AMOUNT    BONOD_DETAILS_ID
    10000           3
    1520            4

enter image description here

查询2

SELECT SUM( AMOUNT ) THE_SUM, BONOD_DETAILS_ID
FROM EXPENSES_TABLE
WHERE BONOD_DETAILS_ID
IN (
SELECT BONOD_DETAILS_ID
FROM BONOD_DETAILS
WHERE USER_ID =16
AND BONOD_ENABLE =1
) GROUP BY BONOD_DETAILS_ID
UNION ALL
SELECT 0 AS THE_SUM, BONOD_DETAILS_ID
FROM BONOD_DETAILS
WHERE BONOD_ID
IN (
SELECT BONOD_ID
FROM BONOD_TABLE
WHERE USER_ID =16
)
AND BONOD_DETAILS_ID NOT 
IN (SELECT BONOD_DETAILS_ID
FROM EXPENSES_TABLE
)

结果是:

 THE_SUM    BONOD_DETAILS_ID
 270        3
 0          4

enter image description here

因此,我想从#Query1中的BONOD_AMOUNT中减去查询 和#Query2中的BONOD_AMOUNT

结果可以是:

 THE_SUB    BONOD_DETAILS_ID
 9730       3
 1520       4

2 个答案:

答案 0 :(得分:1)

最简单的方法是将BONOD_DETAILS_ID上的这两个连接在一起,然后减去第一列:

SELECT
    sub1.BONOD_AMOUNT - sub2.THE_SUM AS THE_SUB, sub1.BONOD_DETAILS_ID
FROM
    (
        SELECT BONOD_AMOUNT, BONOD_DETAILS_ID
        FROM  `BONOD_DETAILS` 
        WHERE BONOD_ID
        IN (
        SELECT BONOD_ID
        FROM BONOD_TABLE
        WHERE USER_ID =16
        )
    )sub1
    LEFT OUTER JOIN 
    (
        SELECT SUM( AMOUNT ) THE_SUM, BONOD_DETAILS_ID
        FROM EXPENSES_TABLE
        WHERE BONOD_DETAILS_ID
        IN (
        SELECT BONOD_DETAILS_ID
        FROM BONOD_DETAILS
        WHERE USER_ID =16
        AND BONOD_ENABLE =1
        ) GROUP BY BONOD_DETAILS_ID
        UNION ALL
        SELECT 0 AS THE_SUM, BONOD_DETAILS_ID
        FROM BONOD_DETAILS
        WHERE BONOD_ID
        IN (
        SELECT BONOD_ID
        FROM BONOD_TABLE
        WHERE USER_ID =16
        )
        AND BONOD_DETAILS_ID NOT 
        IN (SELECT BONOD_DETAILS_ID
        FROM EXPENSES_TABLE
        )
    ) sub2 ON sub1.BONOD_DETAILS_ID = sub2.BONOD_DETAILS_ID

我想可以简化一下,但是由于我看不到您的数据,也不知道表之间的关系,因此不能肯定地说。我可以说,这将获得您想要的结果。

答案 1 :(得分:0)

我相信您的#Query2中有一个小错误。在工会的各个部分。 我认为要工作,您应该执行以下操作:

SELECT 0 AS THE_TOTAL_SPENT , BONOD_DETAILS_ID
FROM BONOD_DETAILS
WHERE BONOD_ID
IN (
SELECT BONOD_ID
FROM BONOD_TABLE
WHERE USER_ID =16
)
AND BONOD_DETAILS_ID NOT 
IN (
SELECT BONOD_DETAILS_ID
FROM EXPENSES_TABLE WHERE USER_ID = 16
)

所以,正如JNevill回答的那样。查询将是

SELECT A.TOTAL_AMOUNT-B.THE_TOTAL_SPENT AS TOTAL, 
A.TOTAL_AMOUNT, B.THE_TOTAL_SPENT,A.BONOD_DETAILS_ID
FROM
(SELECT BONOD_AMOUNT TOTAL_AMOUNT , BONOD_DETAILS_ID
FROM  `BONOD_DETAILS ` 
WHERE BONOD_ID
IN ( SELECT BONOD_ID
FROM BONOD_TABLE
WHERE USER_ID =16
)) A, 
( SELECT SUM( AMOUNT ) THE_TOTAL_SPENT, BONOD_DETAILS_ID
fROM EXPENSES_TABLE
WHERE BONOD_DETAILS_ID
IN (
SELECT BONOD_DETAILS_ID
FROM BONOD_DETAILS
WHERE USER_ID =16
AND BONOD_ENABLE =1
)
GROUP BY BONOD_DETAILS_ID
UNION ALL 
SELECT 0 AS THE_TOTAL_SPENT , BONOD_DETAILS_ID
FROM BONOD_DETAILS
WHERE BONOD_ID
IN (
SELECT BONOD_ID
FROM BONOD_TABLE
WHERE USER_ID =16
)
AND BONOD_DETAILS_ID NOT 
IN (
SELECT BONOD_DETAILS_ID
FROM EXPENSES_TABLE WHERE USER_ID = 16
)
) B
WHERE A.BONOD_DETAILS_ID = B.BONOD_DETAILS_ID