You need to set alias for the table for which you want to delete as shown in below example as w
is used for WorkRecord2
. It is required when needed with inner join delete or update statements.
DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND Date = '2013-05-06'
Also only one table delete is allowed. Deleting with more than one table is allowed in MySQL not MSSQL. You can find the live demo Here.
Below is the query with sample data.
create table WorkRecord2 (EmployeeRun int, empWorkNo varchar(10))
create table Employee (EmployeeNo int, empWorkNo varchar(10), company int, createdDate datetime)
insert into WorkRecord2 Values (1, 'EMW101'),(2, 'EMW102'),(3, 'EMW103'),(4, 'EMW104'),(5, 'EMW105')
insert into Employee values(1, 'EMW101', 1, GETDATE()),(2, 'EMW103', 2, GETDATE())
Select * from WorkRecord2
Select * from Employee
-- This will delete with employeeRun 1 and empWorkNo EMW101
DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
ON EmployeeRun=EmployeeNo
WHERE Company = 1 and Cast(createdDate as DATE) = Cast(GETDATE() as DATE)
Select * from WorkRecord2
Select * from Employee
Before delete the table data is as shown below.
EmployeeRun empWorkNo
---------------------
1 EMW101
2 EMW102
3 EMW103
4 EMW104
5 EMW105
After delete with inner join the table data is as shown blow.
EmployeeRun empWorkNo
---------------------
2 EMW102
3 EMW103
4 EMW104
5 EMW105