gpt4 book ai didi

mysql - 在mysql中使用内部连接在同一个表中添加不同的列

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

我想对特定项目的时间条目进行报告。我尝试了以下查询。

表 1:项目

id   | Name
------------
1 | A
2 | B

表2:EmployeeTimeEntry

proj | activity  |time
----------------------
1 | coding | 5
2 | coding | 2
1 | testing | 2
1 | coding | 2

我想要的项目 A 输出:

proj  | TotalDur  | activity | Activitytime
--------------------------------------------
A | 9 | coding | 7
A | 9 | testing | 2

我的查询:

$query = "SELECT        
name as 'Proj',
TimeEntry.Total as 'TotalDur',
ATimeEntry.ADetails as 'activity',
ATimeEntry.ATotal as 'Activitytime'
FROM Projects pr

INNER JOIN(SELECT project,SUM(time) as Total from EmployeeTimeEntry group by project ) TimeEntry on pr.id = TimeEntry.project
INNER JOIN(SELECT project,details as ADetails,SUM(time) as ATotal from EmployeeTimeEntry where id = pr.id group by details ) ATimeEntry on pr.id = TimeEntry.project";

但是我得到的输出是

proj  | TotalDur  | activity | Activitytime
--------------------------------------------
A | 9 | coding | 9
A | 9 | testing | 2

添加所有项目的所有事件时间。 我使用组合来选择要显示报告的项目。

最佳答案

我觉得你把它复杂化了

select
p.name as Proj,
x.TotalDur,
et.activity,
sum(et.time) as Activitytime
from Projects p
join (
select proj, sum(time) as TotalDur from EmployeeTimeEntry group by proj
)x on x.proj = p.id
join EmployeeTimeEntry et on et.proj = p.id
where p.name = 'A'
group by p.name,et.activity

DEMO

关于mysql - 在mysql中使用内部连接在同一个表中添加不同的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28105937/

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