简体   繁体   English

mysql UNION错误结果?

[英]mysql UNION wrong result?

I have query like this 我有这样的查询

(SELECT s1.m as m,IFNULL(s2.y,2013) as y,IFNULL(s2.s,0) as planned FROM
(SELECT 1 as m 
UNION SELECT 2 
UNION SELECT 3 
UNION SELECT 4 
UNION SELECT 5 
UNION SELECT 6 
UNION SELECT 7 
UNION SELECT 8 
UNION SELECT 9 
UNION SELECT 10 
UNION SELECT 11   
UNION SELECT 12) s1
LEFT JOIN
(SELECT YEAR(p.pdate2) as y,MONTH(p.pdate2) as m, SUM(p.totwdisc) as s FROM   pro_partial_inv p WHERE YEAR(p.pdate2)=2013 GROUP BY y,m) s2
ON s1.m=s2.m)
UNION
(SELECT s5.m as m,IFNULL(s3.y,2013) as y,IFNULL(s3.s,0) as realv FROM
(SELECT 1 as m 
UNION SELECT 2 
UNION SELECT 3 
UNION SELECT 4 
UNION SELECT 5 
UNION SELECT 6 
UNION SELECT 7 
UNION SELECT 8 
UNION SELECT 9 
UNION SELECT 10 
UNION SELECT 11   
UNION SELECT 12) s5
LEFT JOIN
(SELECT YEAR(p.paidd) as y,MONTH(p.paidd) as m, SUM(p.totwdisc) as s FROM  pro_partial_inv p WHERE p.status=2 AND YEAR(p.paidd)=2013 GROUP BY y,m) s3
ON s5.m=s3.m)
UNION
(SELECT s6.m as m,IFNULL(s4.y,2013) as y,IFNULL(s4.s,0) as proj FROM
(SELECT 1 as m 
UNION SELECT 2 
UNION SELECT 3 
UNION SELECT 4 
UNION SELECT 5 
UNION SELECT 6 
UNION SELECT 7 
UNION SELECT 8 
UNION SELECT 9 
UNION SELECT 10 
UNION SELECT 11   
UNION SELECT 12) s6
LEFT JOIN
(SELECT YEAR(p.startd) as y,MONTH(p.startd) as m, SUM(p.disc_n) as s FROM pro_project  p WHERE YEAR(p.startd)=2013 GROUP BY y,m) s4
ON s6.m=s4.m)

So I need it to be like this: 所以我需要像这样:

m       y     planned

1   2013    0.00    
2   2013    0.00    
3   2013    0.00    
4   2013    0.00    
5   2013    0.00    
6   2013    908.18    
7   2013    0.00    
8   2013    1136.36    
9   2013    13354.54    
10  2013    0.00    
11  2013    0.00    
12  2013    0.00    
1   2013    0.00    
2   2013    0.00    
3   2013    0.00    
4   2013    0.00    
5   2013    0.00    
6   2013    908.18    
7   2013    0.00    
8   2013    1136.36    
9   2013    13354.54    
10  2013    0.00    
11  2013    0.00    
12  2013    0.00
1   2013    0.00    
2   2013    0.00    
3   2013    0.00    
4   2013    0.00    
5   2013    0.00    
6   2013    908.18    
7   2013    0.00    
8   2013    1136.36    
9   2013    13354.54    
10  2013    0.00    
11  2013    0.00    
12  2013    0.00

it is like 3 unions that look very similar but are different. 就像3个工会看起来非常相似但有所不同。 m represents month, y year and planned values from calculation. m表示月份,y年和计算得出的计划值。 The result I get is this: 我得到的结果是这样的:

m       y     planned

1   2013    0.00    
2   2013    0.00    
3   2013    0.00    
4   2013    0.00    
5   2013    0.00    
6   2013    908.18    
7   2013    0.00    
8   2013    1136.36    
9   2013    13354.54    
10  2013    0.00    
11  2013    0.00    
12  2013    0.00
6   2013    0.00
7   2013    809.09
8   2013    2227.27
9   2013    0.00
7   2013    43600.00
8   2013    41330.00

Am I doing something wrong? 难道我做错了什么? probably am but I am not sure why is here not made union 3 times, when I run the queries separated I get good result. 可能是,但我不确定为什么这里不进行3次合并,当我运行分开的查询时,我会得到很好的结果。

I suspect the two UNION s need to be changed to UNION ALL between the three "major" queries. 我怀疑在三个“主要”查询之间需要将两个UNION更改为UNION ALL That prevents duplicates from being removed. 这样可以防止重复项被删除。

Yes! 是! I think you can replace each of the 3 inner sections with something like : 我认为您可以将3个内部部分替换为:

(SELECT s2.m as m,IFNULL(s2.y,2013) as y,IFNULL(s2.s,0) as planned FROM
(SELECT YEAR(p.pdate2) as y,MONTH(p.pdate2) as m, SUM(p.totwdisc) as s FROM pro_partial_inv p WHERE YEAR(p.pdate2)=2013 GROUP BY y,m) s2
WHERE s2.m in (2,3,4,5,6,7,8,9,10,11,12))

or even this: 甚至这个:

(SELECT s2.m as m,IFNULL(s2.y,2013) as y,IFNULL(s2.s,0) as planned FROM
(SELECT YEAR(p.pdate2) as y,MONTH(p.pdate2) as m, SUM(p.totwdisc) as s FROM pro_partial_inv p WHERE YEAR(p.pdate2)=2013 GROUP BY y,m) s2
WHERE s2.m != 1)

in fact - correct me if I've oversimplified and lost something - but I think you can reduce each inner section to : 实际上-如果我过于简化并丢失了一些内容,请指正我-但是我认为您可以将每个内部部分简化为:

SELECT 
    YEAR(p.pdate2) as y,
    MONTH(p.pdate2) as m, 
    SUM(p.totwdisc) as planned
FROM pro_partial_inv p 
WHERE y=2013 AND m != 1
GROUP BY y,m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM