I'd like to show the latest action from each task. This is the table (dummy data but same structure) :
//t_task
task_id task_name
A1 PC Proc
A2 Printer Proc
A3 Stationery Proc
//t_task_d
task_id assigned_to
A1 John
A1 Sally
A2 John
A3 Sally
//t_act
no act_id act_date task_id
1 C1 2017-07-10 A1
2 C2 2017-07-14 A1
3 C3 2017-07-17 A1
4 C1 2017-07-21 A2
//t_act_d
act_id act_name
C1 Surveying
C2 Contract
C3 Execution
From the above tables, I'd like to create some kind of report. This is my expected output:
no task_name dates_of_act status
1 PC Proc 2017-07-17 Execution
2 Printer Proc 2017-07-21 Surveying
3 Stationery Proc - Pending /*if it's NULL, then it should be pending, but I can change this in the PHP section*/
This is my closest current query:
SELECT
t_task.task_name,
DATE(t_act.act_date) AS 'dates_of_act',
t_act_d.act_name
FROM t_task
INNER JOIN t_task_d ON t_task.task_id = t_task_d.task_id
LEFT OUTER JOIN t_act ON t_task.task_id = t_act.task_id
LEFT OUTER JOIN t_act_d ON t_act.act_id = t_act_d.act_id
GROUP BY t_task.task_id
ORDER BY t_act.act_date ASC
My query result is:
no task_name dates_of_act status
1 PC Proc 2017-07-10 Surveying
2 Printer Proc 2017-07-21 Surveying
3 Stationery Proc - Pending
Note
I prefer speed because the data is huge. I also try to avoid subqueries if possible