Jump to content

How to insert into select statement using 2 tables?

- - - - -

  • Please log in to reply
5 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 474 posts
I have employee table for attendance and i have a test select statement to get the total hours of employee:


select  sec_to_time(SUM(unix_timestamp(timeout) - unix_timestamp(timein))) AS totalhours from employee;


and now i want to insert the sum of hours per employee in time database with the employee no.

here is my code:

INSERT INTO time (empno,total)

SELECT EMP_NO,sec_to_time(SUM(unix_timestamp(timeout) - unix_timestamp(timein)))

FROM employee

GROUP BY EMP_NO;


no data inserted in time database. What's erong in my query?
Thank you

Edited by Alyn, 25 October 2011 - 07:53 PM.
fixed code tag


#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Art thou sure it's 'GROUP BY' ? I mean, there's also 'ORDER BY' , but I know not if this is the problem (I don't have as much experience in MySQL, so I am not as good at the debugging).

#3
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 474 posts
Thank you... i will try order by...

#4
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 474 posts
I upload attendance .xml using php and it saves to database.

I tried this data to upload:
100603 10/1/11 5:35 AM 10/1/11 1:35 PM // this is the exact schedule of employee and its 8 hours per day he time in exactly and timeout exactly also, so no problem in computing because it is 8 hours.
100603 10/2/11 5:25 AM 10/2/11 1:55 PM //in this example data the employee time in early in his schedule and time out late. It should be only 8 hours.
100603 10/3/11 5:40 AM 10/3/11 1:40 PM // in this example data the employee time in is late, so even he also late to timeout there’s no exemption because he late on his work. So it should has deduction or minus in his total hours.

and it saves it database:
100603 2011-10-01 05:35:00 2011-10-01 13:35:00
100603 2011-10-02 05:25:00 2011-10-01 13:55:00
100603 2011-10-02 05:40:00 2011-10-01 13:40:00


I want to accomplish is to get the total hours of the employee based on the employee no. And even the employee get timein early before his time or late to timeout the hours computed only 8 hours. Honestly, I don’t have idea how can be possible it is.

and I tried this code for computing the hours per day:

select  sec_to_time(unix_timestamp(timeout) - unix_timestamp(timein)) AS totalhours from employee;


and the result of this code is:

totalhours:
08:00:00
08:30:00
08:00:00



and the result is

the first is correct because the real schedule is 5:35 AM - 1:35 PM
the second is wrong it should be 8 hours only even he timein early and timeout late.
the third is also wrong because the employee is late to timein, even he also timeout late., it should be deduct or subtract in hours the late of employee.

I hope someone can help me. Thank you

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Moderation notice: Your threads about your issue is just about everywhere. please stick to one thread for the single problem.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 474 posts
Ok....I'm Sorry...I will not do it again




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users