...

View Full Version : Insert into select statement using 2 tables



newphpcoder
10-24-2011, 04:59 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

Old Pedant
10-24-2011, 05:50 AM
One possible problem: time is a keyword. Maybe you simply need to put `...` (back ticks...shares the key with umlaut ~ character) around that name?


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


If that doesn't fix it, double check to make sure the SELECT is working as it is in that INSERT.

That is, do

SELECT EMP_NO,sec_to_time(SUM(unix_timestamp(timeout) - unix_timestamp(timein)))
FROM employee
GROUP BY EMP_NO;

Does that get the right values?

newphpcoder
10-24-2011, 06:05 AM
One possible problem: time is a keyword. Maybe you simply need to put `...` (back ticks...shares the key with umlaut ~ character) around that name?


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


If that doesn't fix it, double check to make sure the SELECT is working as it is in that INSERT.

That is, do

SELECT EMP_NO,sec_to_time(SUM(unix_timestamp(timeout) - unix_timestamp(timein)))
FROM employee
GROUP BY EMP_NO;

Does that get the right values?

It only insert the empno..the total did not insert..but when I run the select statement it works. the datatype of total is datetime. So the insert data in total is 0000-00-00 00:00:00

Thank you

newphpcoder
10-24-2011, 06:12 AM
When I change the datatype of total from datetime to time the sum of timein and timeout display...is it correct?But theres a row added and the total data is : 00:00:00
and how can i display using php the total in hour and minutes only? Thank you

newphpcoder
10-24-2011, 06:21 AM
here is the vardump of my database:


-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version 5.1.41


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;


--
-- Create schema db_upload
--

CREATE DATABASE IF NOT EXISTS db_upload;
USE db_upload;

--
-- Definition of table `db_upload`.`employee`
--

DROP TABLE IF EXISTS `db_upload`.`employee`;
CREATE TABLE `db_upload`.`employee` (
`EMP_NO` varchar(50) NOT NULL,
`timein` datetime NOT NULL,
`timeout` datetime NOT NULL,
`totalhours` datetime NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `db_upload`.`employee`
--

/*!40000 ALTER TABLE `employee` DISABLE KEYS */;
INSERT INTO `db_upload`.`employee` (`EMP_NO`,`timein`,`timeout`,`totalhours`) VALUES
('100603','2011-10-01 05:35:00','2011-10-01 13:35:00','0000-00-00 00:00:00'),
('100603','2011-10-02 05:25:00','2011-10-02 13:55:00','0000-00-00 00:00:00'),
('100603','2011-10-03 05:40:00','2011-10-03 13:40:00','0000-00-00 00:00:00'),
('','0000-00-00 00:00:00','0000-00-00 00:00:00','0000-00-00 00:00:00');
/*!40000 ALTER TABLE `employee` ENABLE KEYS */;


--
-- Definition of table `db_upload`.`time`
--

DROP TABLE IF EXISTS `db_upload`.`time`;
CREATE TABLE `db_upload`.`time` (
`empno` varchar(15) NOT NULL,
`total` time NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `db_upload`.`time`
--

/*!40000 ALTER TABLE `time` DISABLE KEYS */;
INSERT INTO `db_upload`.`time` (`empno`,`total`) VALUES
('100603','24:30:00'),
('','00:00:00');
/*!40000 ALTER TABLE `time` ENABLE KEYS */;




/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum