Log in

View Full Version : Insert into select statement using 2 tables



newphpcoder
10-24-2011, 04:00 AM
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:
[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;
[/CODE]

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

fobos
12-14-2011, 04:39 PM
Change the SELECT statement to VALUES



INSERT INTO time (empno,total)
SELECT EMP_NO,sec_to_time(SUM(unix_timestamp(timeout) - unix_timestamp(timein)))
FROM employee
GROUP BY EMP_NO;




$con = mysql_connect("localhost","peter","abc123");
mysql_select_db("my_db", $con);
INSERT INTO time (empno, total)
VALUES (EMP_NO, sec_to_time(SUM(unix_timestamp(timeout) - unix_timestamp(timein)))


Also, make sure that your mysql database table is set to auto increment.