删除SQL语法时出错

时间:2013-07-03 20:48:35

标签: mysql sql sqlyog

好的家伙,我需要你的帮助我在mysql语法中遇到麻烦

我有一张

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
);

我想删除它:

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'

这是我的错误:

  

错误代码:1064您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   'WHERE t.employeeID = e.employeeID和e.employeelname附近   ='EnterNumber'和dateoft'在第1行

2 个答案:

答案 0 :(得分:0)

您的表格应以 RELATIONAL 方式设置:

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 =单身员工,多次

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

答案 1 :(得分:0)

第一件事:

DELETE FROM tblemployee t,tblemployee e

这些都是相同的表,所以这就是你收到错误的原因。猜猜应该是:

DELETE FROM tbltimpunch t,tblemployee e

因为您要从多个表中删除,所以查询应该是这样的:

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'