简体   繁体   English

MySQL Sum列IF ID在另一个表查询中

[英]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.

相关问题 Mysql 按 id 对列组求和,然后将每个 id 的总和与另一个表中另一列的值相除,每行的 id 匹配 - Mysql sum a column group by id, then divide that sum for each id with value from another column in another table with the matching id for each row MySQL Join 查询根据另一表中对应的登录ID对一个表中的字段求和 - MySQL Join Query to Sum a Field in One Table based on Its Corresponding Login ID in another Table MySQL查询求和另一列条件 - Mysql query sum another column with a condition MySQL查询顺序按另一个表的SUM字段 - MySQL query order by SUM field of another table 用案例查询的MySQL总和输出另一个表 - MySql Sum with Case query to output another table Mysql查询用子句从另一个表中的值中减去表中列的总和 - Mysql query to subtract sum of a column in a table from a value in another table with clauses 将另一个表列的总和添加到查询中 - Adding the sum of another table column to a query MYSQL:选择列的 SUM 但该列基于另一个行 ID - MYSQL:Selecting SUM of a column but the column is based of another row ID MySQL查询汇总表中的一列,其中两个列组合在一起时是唯一的 - Mysql query to sum a column from a table where another two columns are unique when combined together MYSQL 查询检查 ID 是否存在并将列行值复制到另一个表相关的列名 - MYSQL query check if ID exist and copy column row values to another table related column names
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM