문제

ok guys i need your help im having trouble in mysql syntax

i have a table of

CREATE TABLE tblemployee(
`employeeID` int(5) Primary key not null,
`employeefname` varchar(15),
`employeelname` varchar(15),
);

CREATE TABLE tbltimpunch(
`employeeID` varchar(10),
`dateoftime` datetime,
`timein` time,
`timeout` time
);

and i want to delete this:

DELETE FROM tblemployee t,tblemployee e 
WHERE t.employeeID = e.employeeID 
and e.employeelname ='EnterNumber'
and dateoftime ='2013-07-02' 
and timein ='09:00:00' 
and timeout = '15:00:00'

this is my error:

Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE t.employeeID = e.employeeID and e.employeelname ='EnterNumber' and dateoft' at line 1

도움이 되었습니까?

해결책

First thing:

DELETE FROM tblemployee t,tblemployee e

These are both same tables, so thats why you're getting error. Guess it should be:

DELETE FROM tbltimpunch t,tblemployee e

Because you're deleting from multiple tables, query should be something like this:

DELETE t, e 
FROM tbltimpunch t 
INNER JOIN tblemployee e  
WHERE CAST(t.employeeID AS SIGNED) = e.employeeID  
AND e.employeelname ='EnterNumber' 
AND dateoftime ='2013-07-02'  
AND timein ='09:00:00'  
AND timeout = '15:00:00'

다른 팁

Your tables should be setup in a RELATIONAL way:

CREATE TABLE IF NOT EXISTS `employees` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `firstName` varchar(64) NOT NULL,
  `lastName` varchar(64) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `employee_times` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `employee_id` int(11) NOT NULL,
  `timein` datetime NOT NULL,
  `timeout` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `employee_id` (`employee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

ALTER TABLE `employee_times`
  ADD CONSTRAINT `employee_times_ibfk_1` FOREIGN KEY (`employee_id`) REFERENCES `employees` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Now when you simply delete from employees, the effect cascades into employee_times.

Also notice the table names:

employees = multiple employees

employee_times = single employee, multiple times

This is how you make a database relational.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top