我有一個表tbl_worklog
與列id(int),project_id(int),start_time(datetime),total(時間),user_id(id)。使用mysql每日選擇數據
我還有另一個表tbl_projects
列id(int),project_name(文本),organization_id(int)。第一個表中的project_id對第二個表中的id有一個外鍵。
我也有organization
表的列created_by(int)。組織表中的id對第二個表中的organization_id具有外鍵。
因此,這裏的問題,我想所有的項目成員,是CREATED_BY = 1,而在每一個成員,我需要每天得到他們的第一臺工作的組織下。例如,
Members | Sun | Mon | Tue | Wed | Thu | Fri | Sat | Weekly Total
---------------
John | 0:30:00 | 0:30:00 | 0:30:00 | 0:30:00 | 0:30:00 | 0:30:00 | 1:00:00 | 4:00:00
我在第一個表中使用了start_time來計算他們的日常工作。
這裏是我的查詢不工作,
SELECT (SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 7 DAY) AND start_time >= '2016-09-04' AND user_id=tbl_worklog.user_id) as weekly_total,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 1 DAY) AND start_time >= '2016-09-04' AND user_id=tbl_worklog.user_id) as sun,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 2 DAY) AND start_time >= date_add('2016-09-04', INTERVAL 1 DAY) AND user_id=tbl_worklog.user_id) as mon,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 3 DAY) AND start_time >= date_add('2016-09-04', INTERVAL 2 DAY) AND user_id=tbl_worklog.user_id) as tue,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 4 DAY) AND start_time >= date_add('2016-09-04', INTERVAL 3 DAY) AND user_id=tbl_worklog.user_id) as wed,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 5 DAY) AND start_time >= date_add('2016-09-04', INTERVAL 4 DAY) AND user_id=tbl_worklog.user_id) as thu,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 6 DAY) AND start_time >= date_add('2016-09-04', INTERVAL 5 DAY) AND user_id=tbl_worklog.user_id) as fri,
(SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(total))) AS total_time from tbl_worklog WHERE start_time < date_add('2016-09-04', INTERVAL 7 DAY) AND start_time >= date_add('2016-09-04', INTERVAL 6 DAY) AND user_id=tbl_worklog.user_id) as sat,
(SELECT name FROM tbl_accounts WHERE id=tbl_worklog.user_id) as name FROM tbl_worklog WHERE project_id IN (SELECT id FROM tbl_projects WHERE organization_id=1)
有也任何其他方式使之成爲簡單更好的做法? 如何使用PDO來做到這一點?
是的。你提到PHP,所以處理那裏的顯示問題。更加靈活和可擴展(雖然在這種情況下規模確實不是問題) – Strawberry