gpt4 book ai didi

mysql - 联合查询返回意外结果

转载 作者:行者123 更新时间:2023-11-29 01:52:17 25 4
gpt4 key购买 nike

我组合了两个查询以获得我需要的结果,但它没有给出我想要的结果。

查询是这样的

SELECT COALESCE(SUM(rd.AMOUNT),0) as total from revenue_d AS rd JOIN  revenue_m AS rem on rd.RV_ID = rem.RV_ID Where MONTH(rd.SCROLL_DATE) = 5   and YEAR(rd.SCROLL_DATE)= 2016

Union

SELECT COALESCE(SUM(rd.AMOUNT),0) as total from revenue_d AS rd JOIN revenue_m AS rem on rd.RV_ID = rem.RV_ID Where MONTH(rd.SCROLL_DATE) = 6 and YEAR(rd.SCROLL_DATE)= 2016;

它给我的结果是这样的

 +---------------+
| total |
+---------------+
| 0 |
+---------------+

虽然我想要这样

 +---------------+
| total |
+---------------+
| 0 |
+---------------+
| 0 |
+---------------+

甚至使用 Union ALL 但仍然得到相同的结果。

最佳答案

我认为总是执行最后一个查询,所以你应该这样写,

(first Query) UNION ALL (second query);

试试这个查询

(SELECT COALESCE(SUM(rd.AMOUNT),0) AS total 
FROM revenue_d AS rd
JOIN revenue_m AS rem ON rd.RV_ID = rem.RV_ID
WHERE MONTH(rd.SCROLL_DATE) = 5 AND YEAR(rd.SCROLL_DATE)= 2016)

UNION ALL

(SELECT COALESCE(SUM(rd.AMOUNT),0) AS total
FROM revenue_d AS rd
JOIN revenue_m AS rem ON rd.RV_ID = rem.RV_ID
WHERE MONTH(rd.SCROLL_DATE) = 6 AND YEAR(rd.SCROLL_DATE)= 2016);

关于mysql - 联合查询返回意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38409785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com