0

我有一个具体的问题。我想获取账单数据。数据将用于图表,因此需要总和。这是解释:

Sub1 获取本月支付的账单金额总和。

Sub2 获取本月未支付账单的金额总和。

Sub3 获取计算为本月已支付但实际未支付的账单金额总和。这是如果其他公司 5 年或更长时间不支付账单的情况下,该账单是“类似”支付的(法院程序在那之后进行)。

问题是,例如,如果我没有 sub1 的记录,那么 sub2 或 sub3 就无法加入,我想在任何情况下都显示它们。如果 sub2 没有记录而 sub3 有,则 sub3 不会显示,这也是问题所在。即使 sub1 有 NULL 值,sub 2 和 sub3 有 NOT NULL 值,我如何才能在一行中显示所有记录,反之亦然。任何帮助将不胜感激,因为我现在几乎整个下午都在研究这个查询解决方案。:/

SELECT sub1.y,
       sub1.m,
       sub1.val1,
       sub2.val2,
       sub2.y,
       sub2.m,
       sub3.y,
       sub3.m,
       sub3.val3
FROM
  (SELECT YEAR(curdate()) AS y,
          MONTH(curdate()) AS m,
          SUM(totwdisc) AS val1
   FROM pro_partial_inv
   WHERE paidd IS NOT NULL
     AND bringbar=0
     AND YEAR(curdate())=YEAR(paidd)
     AND MONTH(curdate())=MONTH(paidd)
   GROUP BY y,
            m) sub1
INNER JOIN
  (SELECT YEAR(curdate()) AS y,
          MONTH(curdate()) AS m,
          SUM(netto) AS val2
   FROM pro_partial_inv
   WHERE paidd IS NULL
     AND bringbar=0
     AND YEAR(curdate())=YEAR(pdate2)
     AND MONTH(curdate())=MONTH(pdate2)
   GROUP BY y,
            m) sub2
LEFT JOIN
  (SELECT YEAR(curdate()) AS y,
          MONTH(curdate()) AS m,
          SUM(totwdisc) AS val3
   FROM pro_partial_inv
   WHERE paidd IS NOT NULL
     AND bringbar=1
     AND YEAR(curdate())=YEAR(pdate2)
     AND MONTH(curdate())=MONTH(pdate2)
   GROUP BY y,
            m) sub3 
ON sub1.y=sub2.y
AND sub1.m=sub2.m
AND sub2.y=sub3.y
AND sub2.m=sub3.m
4

3 回答 3

1

有一个最初的选择只是得到年/月,然后离开加入其他反对它?

SELECT sub0.y,
       sub0.m,
       sub1.val1,
       sub2.val2,
       sub2.y,
       sub2.m,
       sub3.y,
       sub3.m,
       sub3.val3
FROM  (SELECT YEAR(curdate()) AS y, MONTH(curdate()) AS m) Sub0
LEFT JOIN(SELECT YEAR(curdate()) AS y,
          MONTH(curdate()) AS m,
          SUM(totwdisc) AS val1
   FROM pro_partial_inv
   WHERE paidd IS NOT NULL
     AND bringbar=0
     AND YEAR(curdate())=YEAR(paidd)
     AND MONTH(curdate())=MONTH(paidd)
   GROUP BY y,
            m) sub1
ON sub0.y=sub1.y
AND sub0.m=sub1.m
LEFT JOIN
  (SELECT YEAR(curdate()) AS y,
          MONTH(curdate()) AS m,
          SUM(netto) AS val2
   FROM pro_partial_inv
   WHERE paidd IS NULL
     AND bringbar=0
     AND YEAR(curdate())=YEAR(pdate2)
     AND MONTH(curdate())=MONTH(pdate2)
   GROUP BY y,
            m) sub2
ON sub0.y=sub2.y
AND sub0.m=sub2.m
LEFT JOIN
  (SELECT YEAR(curdate()) AS y,
          MONTH(curdate()) AS m,
          SUM(totwdisc) AS val3
   FROM pro_partial_inv
   WHERE paidd IS NOT NULL
     AND bringbar=1
     AND YEAR(curdate())=YEAR(pdate2)
     AND MONTH(curdate())=MONTH(pdate2)
   GROUP BY y,
            m) sub3 
ON sub0.y=sub3.y
AND sub0.m=sub3.m

或简化一下:-

SELECT sub0.y,
       sub0.m,
       sub1.val1,
       sub2.val2,
       sub3.val3
FROM  (SELECT YEAR(curdate()) AS y, MONTH(curdate()) AS m) Sub0
LEFT JOIN(SELECT YEAR(paidd) AS y,
          MONTH(paidd) AS m,
          SUM(totwdisc) AS val1
   FROM pro_partial_inv
   WHERE paidd IS NOT NULL
     AND bringbar=0
   GROUP BY y,
            m) sub1
ON sub0.y=sub1.y
AND sub0.m=sub1.m
LEFT JOIN
  (SELECT YEAR(pdate2) AS y,
          MONTH(pdate2) AS m,
          SUM(netto) AS val2
   FROM pro_partial_inv
   WHERE paidd IS NULL
     AND bringbar=0
   GROUP BY y,
            m) sub2
ON sub0.y=sub2.y
AND sub0.m=sub2.m
LEFT JOIN
  (SELECT YEAR(pdate2) AS y,
          MONTH(pdate2) AS m,
          SUM(totwdisc) AS val3
   FROM pro_partial_inv
   WHERE paidd IS NOT NULL
     AND bringbar=1
   GROUP BY y,
            m) sub3 
ON sub0.y=sub3.y
AND sub0.m=sub3.m

或者更简化

SELECT YEAR(curdate()) AS y, 
        MONTH(curdate()) AS m,
        SUM(IF(paidd IS NOT NULL AND bringbar=0, totwdisc, 0)),
        sub2.val2,
        sub2.val3
FROM  pro_partial_inv
LEFT JOIN
  (SELECT YEAR(pdate2) AS y,
          MONTH(pdate2) AS m,   
          SUM(IF(paidd IS NULL AND bringbar=0, netto, 0)) AS val2,
          SUM(IF(paidd IS NOT NULL AND bringbar=0, totwdisc, 0)) AS val3
   FROM pro_partial_inv
   GROUP BY y,
            m) sub2
ON sub0.y=sub2.y
AND sub0.m=sub2.m
GROUP BY y, m
于 2013-06-26T13:26:31.470 回答
0

您不需要为此使用连接。您可以运行 3 个单独的查询来获得所需的结果。

SELECT 
    (SELECT SUM(netto) FROM ...) as sub1,
    (...) as sub2,
    (...) as sub3

您只需在每个子查询上添加所有适当的 WHERE 子句。

于 2013-06-26T13:23:22.073 回答
0

MySQL中没有FULL JOIN,因此您需要在一组适当的年月对上左连接。在你的情况下,只有一个,所以:

select ...
from (select year(curdate()) as y, month(curdate()) as m) cur
left join (...) as sub1 on cur.y = sub1.y and cur.m = sub1.m
left join (...) as sub2 on cur.y = sub2.y and cur.m = sub2.m
left join (...) as sub3 on cur.y = sub3.y and cur.m = sub3.m;
于 2013-06-26T13:34:55.343 回答