Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用join和group by为同一列检索计算值不同的sql结果?_Mysql_Sql - Fatal编程技术网

Mysql 如何使用join和group by为同一列检索计算值不同的sql结果?

Mysql 如何使用join和group by为同一列检索计算值不同的sql结果?,mysql,sql,Mysql,Sql,嗨。下面我编写了一个查询来检索总小时数、上月总小时数和当月总小时数。所有这些都是从time_entries表的hours列和同一表的Consumed_列计算的。抱歉,如果表格格式不好 以下三个查询给出了正确的结果 问题1 select p.name, FORMAT(sum(te.hours), 2) AS totalhours from projects p left join time_entries te on p.id = te.project_id group by p.id 结果1

嗨。下面我编写了一个查询来检索总小时数、上月总小时数和当月总小时数。所有这些都是从time_entries表的hours列和同一表的Consumed_列计算的。抱歉,如果表格格式不好

以下三个查询给出了正确的结果

问题1

select p.name, 
FORMAT(sum(te.hours), 2) AS totalhours
from projects p
left join time_entries te on p.id = te.project_id
group by p.id
结果1

姓名总小时数 -------- -------- 项目A 4932.18 项目B 534.02

问题2

select p.name, 
FORMAT(sum(te_last_mo.hours), 2) AS totalhours_last_mo
from projects p
left join time_entries te on p.id=te_last_mo.project_id
where te_last_mo.spent_on>=DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') and te_last_mo.spent_on<DATE_FORMAT(NOW() ,'%Y-%m-1')
group by p.id
结果2

姓名总小时数最后一个月 -------- --------------- 项目A 1726.72 项目B 157.75

问题3

select p.name, 
FORMAT(sum(te_this_mo.hours), 2) AS totalhours_this_mo
from projects p
left join time_entries te_this_mo on p.id=te_this_mo.project_id
where te_this_mo.spent_on>=DATE_FORMAT(NOW() ,'%Y-%m-01') and te_this_mo.spent_on<DATE_FORMAT(NOW() ,'%Y-%m-31')
group by p.id
结果3

名称总时数\u此\u mo -------- --------------- 项目A 421.19 项目B 41.26

以上结果和查询是正确的

现在我想要这样的结果,但我无法理解

名称总时数总时数上次总时数本次总时数 ------ --------------- ---------------- --------------- 项目A 4932.18 1726.72 421.19 项目B 534.02 157.75 41.26

为了组合这三个小时的列,我编写了这样的查询,但抛出了错误的结果,可能是因为对同一个表进行了三次连接

select p.name, 
FORMAT(sum(te.hours), 2) AS totalhours,
FORMAT(sum(te_last_mo.hours), 2) AS totalhours_last_mo,
FORMAT(sum(te_this_mo.hours), 2) AS totalhours_this_mo

from projects p
left join time_entries te on p.id = te.project_id

left join time_entries te_last_mo on p.id = te_last_mo.project_id 
and te_last_mo.spent_on>=DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') and te_last_mo.spent_on<DATE_FORMAT(NOW() ,'%Y-%m-1')

left join time_entries te_this_mo on p.id = te_this_mo.project_id
where te_this_mo.spent_on>=DATE_FORMAT(NOW() ,'%Y-%m-01') and te_this_mo.spent_on<DATE_FORMAT(NOW() ,'%Y-%m-31')

group by p.id

任何解决方案都将不胜感激。提前感谢。

您可以将查询中的数据转储到临时表中,确保每个表中都有项目,然后根据项目查询这些数据以将其全部汇总起来。

尝试以下操作:

select p.name as "name", 
FORMAT(sum(te.hours), 2) AS totalhours, B.totalhours_last_mo, C.totalhours_this_mo
from projects p
left join time_entries te on p.id = te.project_id
group by p.id) AS A LEFT JOIN 
(select p.name as name, 
FORMAT(sum(te_last_mo.hours), 2) AS totalhours_last_mo
from projects p
left join time_entries te on p.id=te_last_mo.project_id
where te_last_mo.spent_on>=DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') and te_last_mo.spent_on<DATE_FORMAT(NOW() ,'%Y-%m-1')
group by p.id) AS B
ON A.name = B.name LEFT JOIN
(select p.name as Name, 
FORMAT(sum(te_this_mo.hours), 2) AS totalhours_this_mo
from projects p
left join time_entries te_this_mo on p.id=te_this_mo.project_id
where te_this_mo.spent_on>=DATE_FORMAT(NOW() ,'%Y-%m-01') and te_this_mo.spent_on<DATE_FORMAT(NOW() ,'%Y-%m-31')
group by p.id) AS C

您可以使用连接而不使用聚合来运行查询,以查看这些连接在一起使用时的工作方式以及为什么会导致错误的结果

通过使用一个联接并将条件移动到聚合计算,可以获得所需的结果:

select p.name, 
FORMAT(sum(te.hours), 2) AS totalhours,
FORMAT(sum(
IF(spent_on>=DATE_FORMAT(NOW() - INTERVAL 1 MONTH, '%Y-%m-01') and spent_on<DATE_FORMAT(NOW() ,'%Y-%m-1'),
hours, NULL)
), 2) AS totalhours_last_mo,
FORMAT(sum(
IF(spent_on>=DATE_FORMAT(NOW() ,'%Y-%m-01') and spent_on<DATE_FORMAT(NOW() ,'%Y-%m-31'),
hours, NULL)
), 2) AS totalhours_this_mo
from projects p
left join time_entries te on p.id = te.project_id
group by p.id

您尝试过联合吗?我没有尝试过联合。由于别名的原因,脚本未运行。如果在select查询后定义别名,则会引发错误,因此无法对其进行测试。您能给我们展示一下类似案例的工作示例吗?非常感谢您的解决方案和清晰的描述。你真的节省了我的时间。