[英]MySQL Sum column IF ID is in another table query
Edit --- 编辑-
I thought I had it, but the below doesn't seem to work. 我以为自己有,但以下内容似乎无效。 It looks like it's totaling everything from the DB.
看起来它正在汇总数据库中的所有内容。
SELECT SUM( drivetime ) AS drivetime, record_id
FROM `workforce`
WHERE EXISTS (
SELECT *
FROM project_info
WHERE date1
BETWEEN 'xx-xx-xxxx'
AND 'xx-xx-xxxx'
)
Will try below suggestions. 将尝试以下建议。
-- end edit -结束编辑
The end goal is to (pseudo) : SELECT SUM(work) from tasks where R_ID is in (select ID from Info where date between X and X) 最终目标是(伪):从R_ID所在的任务中选择SUM(工作)(从X到X之间的日期的Info中选择ID)
Info 信息
+----------------------+
| ID | other fields...|
+-----+----------------+
| 1 | etc... |
+-----+----------------+
| 2 | etc... |
+-----+----------------+
| 3 | etc... |
+-----+----------------+
Tasks 任务
+----------------------+
| R_ID | work| other..|
+-------+-----+--------+
| 1 | 10 | |
+-------+-----+--------+
| 2 | 2 | |
+-------+-----+--------+
| 2 | 2 | |
+-------+-----+--------+
| 3 | 2 | |
+-------+-----+--------+
Our second query returns that 2 & 3 are within the date range. 我们的第二个查询返回2和3在日期范围内。 So the sum would be 6. What is the best way to accomplish this?
因此总和为6。什么是最好的方法?
Everything I've been able to find has hard conditions - not range conditions based on a second select. 我所能找到的所有东西都有艰苦的条件,而不是基于第二选择的范围条件。 I currently get my range from an initial select and then iterate over all records in a given table to sum what's within the range.
目前,我是从初始选择中获得范围的,然后遍历给定表中的所有记录以求和范围内的总和。 This is terribly slow over thousands of records.
这在成千上万的记录上非常慢。
I'll keep looking and post back anything I find / try. 我会继续寻找并回寄我找到/尝试的任何内容。
Thanks for reading! 谢谢阅读!
-- Current code (edited to remove extra fields & data) for posterity. -后代的当前代码(已编辑以删除多余的字段和数据)。 Only tagged mySQL as server side is irrelevant (and is being converted over to PHP).
仅将mySQL标记为服务器端是无关的(并且已将其转换为PHP)。
psql = "SELECT * FROM project_info where approved='yes' where date1 BETWEEN '"& sdate &"' AND '" & edate & "' order by id DESC"
Set prs = Conn.Execute(psql)
do until prs.EOF
sumsql = "SELECT SUM(drivetime) as drivetime from workforce where approved='yes' and report_id='" & prs("ID") & "'"
set sumrs = Conn.Execute(sumsql)
dtime = dtime + cLng(sumrs("drivetime"))
prs.movenext
loop
I think this is a fairly simple join, sum and group by: 我认为这是一个相当简单的连接,求和和分组:
SELECT t.r_id, SUM(t.work) work_sum
FROM tasks t
JOIN info i
ON i.id = t.r_id
AND i.date BETWEEN xxx AND yyy
GROUP BY t.r_id
If you want a null work_sum for tasks with no info in that range: 如果您想要在该范围内没有信息的任务为空的work_sum:
SELECT t.r_id, SUM(t.work) work_sum
FROM tasks t
LEFT JOIN info i
ON i.id = t.r_id
AND i.date BETWEEN xxx AND yyy
GROUP BY t.r_id
If you want 0 work_sum for tasks with no info in that range: 如果您想要0 work_sum用于没有该范围内信息的任务:
SELECT t.r_id, COALESCE(SUM(t.work),0) work_sum
FROM tasks t
LEFT JOIN info i
ON i.id = t.r_id
AND i.date BETWEEN xxx AND yyy
GROUP BY t.r_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.