0

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

4

2 回答 2

0

您的表格应该以关系方式设置:

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;

现在,当您简单地从 中删除时employees,效果会级联到employee_times.

还要注意表名:

employees= 多名员工

employee_ times= 单个员工,多次

这就是建立关系数据库的方式。

于 2013-07-03T21:04:32.947 回答
0

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'
于 2013-07-03T21:07:40.037 回答