[英]How to write this complex MySQL query using three tables?
我有以下三個表:
任務表-task_id,activity_id(FOREIGN KEY),開始日期,結束日期
活動表-activity_id,Milestone_id(FOREIGN KEY),other_fields
里程碑表-milestone_id,project_id,other_fields
現在我想要這樣的結果集
No_of_task(count) | no_activity | milestone_name
5 : 2 : ABC Milestone
我想計算end_dated任務,根據end_date任務和里程碑名稱計算活動。
例如,具有5個活動和20個不同任務的1個里程碑名稱Housing_Construction ... end_dated一些任務
像這樣的housing_construction 3活動的5個任務結束。
任務表說明
+-----------------------+-------------+------+-----+---------+----------------+
| 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 | |
+-----------------------+-------------+------+-----+---------+----------------+
活動表說明
+-----------------------+-------------+------+-----+---------+----------------+
| 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 | |
+-----------------------+-------------+------+-----+---------+----------------+
嘗試這個:
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;
您可以為此目的使用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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.