2016-09-04 53 views
-1

我有一個表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來做到這一點?

+0

是的。你提到PHP,所以處理那裏的顯示問題。更加靈活和可擴展(雖然在這種情況下規模確實不是問題) – Strawberry

回答

0

您沒有使用group by這是最重要的在這裏得到預期的結果。嘗試以下查詢以獲得每日結果。您可以修改以獲取PHP中的每週結果視圖。

select project_name, user_id, sum(time_to_sec(time)) as user_time, date(start_time) as 
daily_time , tw.project_id pID 
inner join tbl_projects tp on tp.id = tw.project_id 
inner join organization og on og.id = tp.organization_id 
where og.created_by = 1 group by user_id, pID, daily_time 
+0

所以這是我的整個查詢?我怎樣才能得到那個成員的星期天工作時間? – JMA

+0

您是否運行了查詢並查看輸出?它會將行作爲天和用戶。您可以使用某些php邏輯將行按列每7天獲得一次 – Riad