我有一个表tbl_projects
,其中包含列id(int),project_id(int),start_time(datetime),total(time),user_id(id)。
我还有另一个表organization
,其中包含列id(int),project_name(text),organization_id(int)。第一个表中的project_id具有第二个表中id的外键。
我还有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
表,其中包含created_by(int)列。组织表中的id具有第二个表中organization_id的外键。
所以这就是问题所在,我希望得到创建_by = 1的组织下的所有项目的成员。在每个成员中,我希望每天在第一个表中获得他们的工作。例如,
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)
我使用第一个表中的start_time来计算他们的日常工作。
这是我的查询无效,
set datafile separator ","
set key autotitle columnhead
set xdata time
set timefmt '"%Y-%m-%d %H:%M:%S"'
set format x "%Y-%m-%d %H:%M:%.6S"
set xrange ['"2016-04-01 00:00:00"':'"2016-05-01 17:00:00"']
plot "~/timestampi.csv" u 2:1
还有其他方法可以让它变得简单而且更好吗? 我怎么能用PDO做到这一点?
答案 0 :(得分: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