2016-12-27 37 views
0

我有以下三個表:如何使用三個表編寫這個複雜的MySQL查詢?

  1. 任務表 - TASK_ID,ACTIVITY_ID(外鍵),日期,結束日期

  2. 活動表 - ACTIVITY_ID,milestone_id(外鍵),other_fields

  3. 里程碑表 - milestone_id,PROJECT_ID,other_fields

現在我想結果S et像這樣

No_of_task(count) | no_activity | milestone_name 
     5   :  2  : ABC Milestone 

我想統計end_dated任務,根據end_date任務和里程碑名稱計數活動。

例如,1里程碑,有5個活動,20個不同的任務名稱Housing_Construction ...一些任務end_dated所以

5任務結束這樣housing_construction的3活性。

任務表的說明

+-----------------------+-------------+------+-----+---------+----------------+ 
| Field     | Type  | Null | Key | Default | Extra   | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| activity_task_id  | int(11)  | NO | PRI | NULL | auto_increment | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| milestone_activity_id | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| task     | varchar(50) | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| description   | text  | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| assign_to_employee_id | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| assign_date   | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| task_end_date   | date  | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| tasktime    | varchar(50) | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| status    | tinyint(1) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| is_delete    | tinyint(1) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| entry_employee_id  | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| edit_employee_id  | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| createddatetime  | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| modifydatetime  | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| nooftimesedit   | smallint(6) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| completion_date  | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 

ActivityTable描述

+-----------------------+-------------+------+-----+---------+----------------+ 
| Field     | Type  | Null | Key | Default | Extra   | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| milestone_activity_id | int(11)  | NO | PRI | NULL | auto_increment | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| project_milestone_id | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| activityname   | varchar(50) | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| activity_end_date  | date  | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| activity_description | text  | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| status    | tinyint(1) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| is_delete    | tinyint(1) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| entry_employee_id  | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| edit_employee_id  | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| createddatetime  | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| modifydatetime  | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| nooftimesedit   | smallint(6) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 

表裏程碑描述

+-----------------------+-------------+------+-----+---------+----------------+ 
| Field     | Type  | Null | Key | Default | Extra   | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| project_milestone_id | int(11)  | NO | PRI | NULL | auto_increment | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| project_id   | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| milestone    | varchar(50) | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| milestone_description | text  | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| milestone_end_date | date  | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| status    | tinyint(1) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| is_delete    | tinyint(1) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| entry_employee_id  | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| edit_employee_id  | int(11)  | YES | MUL | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| createddatetime  | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| modifydatetime  | datetime | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| nooftimesedit   | smallint(6) | YES |  | 0  |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 
| noofdays    | int(11)  | YES |  | NULL |    | 
+-----------------------+-------------+------+-----+---------+----------------+ 

回答

0

試試這個:

select 
    count(distinct t.activity_task_id) No_of_task, 
    count(distinct a.milestone_activity_id) no_activity, 
    m.milestone_description milestone_name 
from milestone m 
inner join activity a 
    on m.project_milestone_id = a.project_milestone_id 
inner join task t 
    on a.milestone_activity_id = t.milestone_activity_id 
where t.status != 4 
and t.task_end_date < curdate() 
group by m.project_milestone_id, 
    m.milestone_description; 
+0

感謝評論,但放在哪裏結束日期條件在此查詢? – Hardik

+0

select t.count as No_of_task from (SELECT COUNT(activity_task_id)count from activity_task_details where status!= 4 and task_end_date Hardik

+0

@hardik請參閱已更新的答案't.end_date' – GurV

0

可以使用JOIN爲了這個目的,使用此下面的代碼,

SELECT COUNT(T.task_id) as no_of_task, 
     COUNT(A.activity_id) as no_of_activity, 
     MAX(M.milestone_name) as milestone_name 
FROM milestone M 
INNER JOIN activity A on M.milestone_id=A.milestone_id 
INNER JOIN task T on T.activity_id =A.activity_id 
GROUP BY A.activity_id,T.task_id 
HAVING T.end_date < curdate() 
+0

謝謝你的回覆,我會試試這個,但是在哪裏可以放置過時的任務?在任務開始日期和結束日期中有兩個字段,我需要在'GROUP BY'之後結束 – Hardik

+0

的任務計數,您可以像這樣放置'HAVING [value1] = 1' –

+0

select t.count as No_of_task from ( SELECT COUNT(activity_task_id)count from activity_task_details where status!= 4 and task_end_date Hardik