gpt4 book ai didi

mysql - 使用 SubQueries 查询 2 个表的总和

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

我的分析团队使用这样的查询:

SELECT 
SUM(ul.upload_bandwidth) + SUM(dl.download_bandwidth) AS total
FROM
upload_facts ul, download_facts dl
WHERE
ul.date BETWEEN '2011-09-01' AND '2011-09-30' AND
dl.date BETWEEN '2011-09-01' AND '2011-09-30';

由于正在发生“加入”,这需要很长时间,超过 12000 秒。

分别从两个表中求和只需几秒钟。我正在考虑将每天的总和放入单独的表格中以加快速度。但是,我相信它应该比这更容易。

我想消除连接并使用子查询来执行此操作,但我不是 100% 确定如何操作。我尝试了以下方法,但没有用。

SELECT 
(select upload_bandwidth from upload_facts where date
BETWEEN '2011-09-01' AND '2011-09-30')
+
(select download_bandwidth from downloaded_facts where date
BETWEEN '2011-09-01' AND '2011-09-30');

最佳答案

SELECT
(SELECT SUM(`upload_bandwidth`) FROM `upload_facts` WHERE `date`
BETWEEN '2011-09-01' AND '2011-09-30')
+
(SELECT SUM(`download_bandwidth`) FROM `download_facts` WHERE `date`
BETWEEN '2011-09-01' AND '2011-09-30')
AS `total`

应该这样做。

关于mysql - 使用 SubQueries 查询 2 个表的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8026097/

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