MYSQL 3表查询;表A中每个的总和表B(如果找到);使用表C(如果找到)对结果进行排序

时间:2018-03-22 23:02:06

标签: mysql

我有3个表(jobs,job_tasks,job_hours)

我希望获得每个作业(如果有)的任务的总和,并根据分配给作业的最近几小时来排序结果。我正在获得所有的工作,而且它正确地命令它们但是当它应该是1时我得到12个taskactive。似乎它不断地为每天工作添加相同的任务。

select MAX(job_hours.job_date) as lastWorked, SUM(job_tasks.active) as taskactive, jobs.job_type
from jobs 
left outer join job_tasks on jobs.idjobs = job_tasks.idjobs
left outer join job_hours on jobs.idjobs = job_hours.idjob
where jobs.active = '1' and jobs.completed = '0'
group by jobs.idjobs 
ORDER BY  job_hours.job_date desc, jobs.job_created_date DESC

results showing incorrect task count

如果我删除job_hours,我得到正确的SUM(job_tasks.active)为1

select SUM(job_tasks.active) as taskactive, jobs.job_type
from jobs 
left outer join job_tasks on jobs.idjobs = job_tasks.idjobs
where jobs.active = '1' and jobs.completed = '0'
group by jobs.idjobs 
ORDER BY jobs.job_created_date DESC

Results showing correct task count

表格工作

 idjobs    job_type                                        active  completed      job_created_date  
  137    Installation, Other (Furnace 28 Rebuild )          1        0        2018-01-23 07:37:36  
  138    Preventive Maintenance (Monthly 2018-February )    1        1        2018-01-23 07:38:12  
  139    Preventive Maintenance (Monthly 2018-March )       1        0        2018-01-23 07:38:20  

表job_tasks

idjob_tasks    idjobs   active  task  
 68              137       1    "Serial Number Verification"  

表job_hours

idjob_hours    idjob    job_date    idemployee  
    190         137    2018-02-21       9  
    197         138    2018-02-21       3  
    198         138    2018-02-21       3  
    199         138    2018-02-20       3  
    201         138    2018-02-22       3  
    202         137    2018-02-19       9  
    203         137    2018-02-20       9  
    204         137    2018-03-08       9  
    205         137    2018-02-23       7  
    206         137    2018-02-19       7  
    207         137    2018-02-20       7  
    209         137    2018-02-22       9  
    216         137    2018-02-22       7  
    217         137    2018-02-21       7  
    218         137    2018-02-21       9  
    220         137    2018-02-23       9  
    255         139    2018-03-12       9  
    260         139    2018-03-13       9  
    261         139    2018-03-14       9  

1 个答案:

答案 0 :(得分:0)

不使用联接中的job_hours表,而是使用子查询从job_hours生成一个表格,该表格仅包含按job_date分组的最新idjob

SELECT MAX(job_hours.job_date) as lastWorked, 
  SUM(job_tasks.active) as taskactive, 
  jobs.job_type 
FROM jobs 
  LEFT OUTER JOIN job_tasks ON jobs.idjobs = job_tasks.idjobs 
  LEFT OUTER JOIN (
    SELECT MAX(job_date) as job_date, idjob 
    FROM job_hours 
    GROUP BY idjob) as job_hours ON job_hours.idjob = jobs.idjobs 
WHERE jobs.active = '1'  
  and jobs.completed = '0' 
GROUP BY jobs.idjobs 
ORDER BY jobs.job_created_date DESC;

此查询将输出:

+------------+------------+----------------------------------------------+
| lastWorked | taskactive | job_type                                     |
+------------+------------+----------------------------------------------+
| 2018-03-08 |          1 | Installation, Other (Furnace 28 Rebuild )    |
| 2018-03-14 |       NULL | Preventive Maintenance (Monthly 2018-March ) |
+------------+------------+----------------------------------------------+