php - Mysql joins 3 tables with multiple fields -
i trying join employee table "employees", timeclock transaction table "transactions" , table "hta" time field. want timeclock table add time employee worked between set of days. want other table "hta" add time each employee between set of days , employee table employees name. make things simple right working 1 day. therefore there 1 entry per employee. no 1 has clocked in multiple times day. in hta table there multiple entries per person day.
here code
select time_format( sec_to_time( sum( time_to_sec( `t`.`hours_today` ) ) ) , "%h:%i:%s" ) `hours_today` , time_format( sec_to_time( sum( time_to_sec( `h`.`run_time` ) ) ) , "%h:%i:%s" ) `run_time` , `t`.`employee_id` , `e`.`employee_id` , `e`.`displayname` `transactions` t left join `hta` h on `t`.`employee_id` = `h`.`employee_id` , `t`.`date` = `h`.`date` left join `employees` e on `t`.`employee_id` = `e`.`employee_id` `t`.`date` >= "2015-08-14" , `t`.`date` <= "2015-08-14" group `t`.`employee_id` order `t`.`employee_id` asc
the output getting hours_today wrong, run_time correct, employee_id correct, , display name correct.
on employee 1 hours_today should 05:48:00. remember employee has 1 entry in hours_today column date range. mysql output employee 29:00:00. looks if date timeclock table not being limited hta table. employee 2 hours_today should 09:34:00 output query 95:40:00
i have tried more detailed code such
select time_format(sec_to_time(sum(time_to_sec(t.hours_today))), "%h:%i:%s") hours_today, t.employee_id, e.employee_id, e.displayname transactions t left join employees e on t.employee_id = e.employee_id left join (select employee_id, time_format(sec_to_time(sum(time_to_sec(run_time))), "%h:%i:%s") run_time hta date(date_time) >= "2015-08-14" , date(date_time) <= "2015-08-14" group employee_id) hta on e.employee_id = hta.employee_id left join hta h on hta.employee_id = h.employee_id date >= "2015-08-14" , date <= "2015-08-14" group t.employee_id order t.employee_id asc
and
select time_format(sec_to_time(sum(time_to_sec(t.hours_today))), "%h:%i:%s") hours_today, time_format(sec_to_time(sum(time_to_sec(h.run_time))), "%h:%i:%s") run_time, t.employee_id, e.employee_id, e.displayname transactions t left join employees e on t.employee_id = e.employee_id left join hta h on e.employee_id = h.employee_id , t.date = date(h.date_time) date >= "2015-08-14" , date <= "2015-08-14" group t.employee_id order t.employee_id asc
after frustration….
select `t1`.`displayname`, `t1`.`employee_id`, `t2`.`hours_today`, `t3`.`run_times` (select `employee_id`, `displayname`, `rate` `employees`) t1 left join (select time_format( sec_to_time( sum( time_to_sec(`hours_today`))) , "%h:%i:%s" ) hours_today, `employee_id` `transactions` `date` = "2015-08-14" group `employee_id`) t2 on `t1`.`employee_id` = `t2`.`employee_id` left join (select time_format( sec_to_time( sum( time_to_sec(`run_time`))) , "%h:%i:%s" ) run_times, `employee_id` `hta` `date` = "2015-08-14" group `employee_id`) t3 on `t1`.`employee_id` = `t3`.`employee_id`
Comments
Post a Comment