我想在 SQL Server 2008 中使用 INNER JOIN
删除。
但我得到这个错误:
消息 156,级别 15,状态 1,第 15 行关键字“INNER”附近的语法不正确。
我的代码:
DELETE
FROM WorkRecord2
INNER JOIN Employee
ON EmployeeRun=EmployeeNo
WHERE Company = '1'
AND Date = '2013-05-06'
DELETE
与联接一起使用
您需要指定要从哪个表中删除。这是一个带有别名的版本:
DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
ON EmployeeRun=EmployeeNo
WHERE Company = '1' AND Date = '2013-05-06'
只需在 DELETE
和 FROM
之间添加要删除记录的表名,因为我们必须指定要删除的表。还要删除 ORDER BY
子句,因为在删除记录时没有要排序的内容。
所以你的最终查询应该是这样的:
DELETE WorkRecord2
FROM WorkRecord2
INNER JOIN Employee
ON EmployeeRun=EmployeeNo
WHERE Company = '1'
AND Date = '2013-05-06';
DELETE Employee
将从 Employee 表中删除,而不是从 WorkRecord2
表中删除。
这可能对您有所帮助-
DELETE FROM dbo.WorkRecord2
WHERE EmployeeRun IN (
SELECT e.EmployeeNo
FROM dbo.Employee e
WHERE ...
)
或者试试这个 -
DELETE FROM dbo.WorkRecord2
WHERE EXISTS(
SELECT 1
FROM dbo.Employee e
WHERE EmployeeRun = e.EmployeeNo
AND ....
)
尝试这个:
DELETE FROM WorkRecord2
FROM Employee
Where EmployeeRun=EmployeeNo
And Company = '1'
AND Date = '2013-05-06'
它应该是:
DELETE zpost
FROM zpost
INNER JOIN zcomment ON (zpost.zpostid = zcomment.zpostid)
WHERE zcomment.icomment = "first"
在 SQL Server Management Studio 中,我可以轻松创建 SELECT
查询:
SELECT Contact.Naam_Contactpersoon, Bedrijf.BedrijfsNaam, Bedrijf.Adres, Bedrijf.Postcode
FROM Contact
INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
我可以执行它,并显示我的所有联系人。
现在将 SELECT
更改为 DELETE
:
DELETE Contact
FROM Contact
INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
您在 SELECT
语句中看到的所有记录都将被删除。
您甚至可以使用相同的过程创建更困难的内部连接,例如:
DELETE FROM Contact
INNER JOIN Bedrijf ON Bedrijf.IDBedrijf = Contact.IDbedrijf
INNER JOIN LoginBedrijf ON Bedrijf.IDLoginBedrijf = LoginBedrijf.IDLoginBedrijf
试试这个查询:
DELETE WorkRecord2, Employee
FROM WorkRecord2
INNER JOIN Employee ON (tbl_name.EmployeeRun=tbl_name.EmployeeNo)
WHERE tbl_name.Company = '1'
AND tbl_name.Date = '2013-05-06';
另一种方法是使用 CTE
:
;WITH cte
AS (SELECT *
FROM workrecord2 w
WHERE EXISTS (SELECT 1
FROM employee e
WHERE employeerun = employeeno
AND company = '1'
AND date = '2013-05-06'))
DELETE FROM cte
注意:当您想要delete
时,我们不能在 CTE
中使用 JOIN
。
你甚至可以做一个子查询。像下面的这段代码:
DELETE FROM users WHERE id IN(
SELECT user_id FROM Employee WHERE Company = '1' AND Date = '2013-05-06'
)
这是我目前用于删除甚至更新的内容:
DELETE w
FROM WorkRecord2 w,
Employee e
WHERE w.EmployeeRun = e.EmployeeNo
AND w.Company = '1'
AND w.Date = '2013-05-06'
这是一次从两个表中删除记录的简单查询。
DELETE table1.* ,
table2.*
FROM table1
INNER JOIN table2 ON table1.id= table2.id where table1.id ='given_id'
您没有为 Company
和 Date
指定表,您可能想要解决这个问题。
使用 MERGE
的标准 SQL:
MERGE WorkRecord2 T
USING Employee S
ON T.EmployeeRun = S.EmployeeNo
AND Company = '1'
AND Date = '2013-05-06'
WHEN MATCHED THEN DELETE;
The answer from Devart 也是标准 SQL,虽然不完整。它应该看起来更像这样:
DELETE
FROM WorkRecord2
WHERE EXISTS ( SELECT *
FROM Employee S
WHERE S.EmployeeNo = WorkRecord2.EmployeeRun
AND Company = '1'
AND Date = '2013-05-06' );
关于上述要注意的重要一点是,很明显删除是针对单个表的,如第二个示例中通过要求标量子查询强制执行的。
对我来说,各种专有语法答案更难阅读和理解。我想the answer by frans eilering 中最好地描述了这种心态,即编写代码的人不一定关心阅读和维护代码的人。
这是我的 SQL Server 版本
DECLARE @ProfileId table(Id bigint)
DELETE FROM AspNetUsers
OUTPUT deleted.ProfileId INTO @ProfileId
WHERE Email = @email
DELETE FROM UserProfiles
WHERE Id = (Select Id FROM @ProfileId)
使用事务块、表变量和JOIN删除多个表数据。
BEGIN TRANSACTION;
declare @deletedIds table ( id int );
DELETE w
output deleted.EmployeeRun into @deletedIds
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06';
DELETE e
FROM Employee as e
INNER JOIN @deletedIds as d
ON d.id = e.EmployeeNo;
COMMIT TRANSACTION;
请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=43330dda6f1b71b8ec4172a24d5b6921 检查
用临时表和JOIN删除多个表数据。删除后删除临时表。
BEGIN TRANSACTION;
-- create temporary table
create table #deletedRecords (employeeId int);
-- INSERT INTO #deletedRecords
SELECT e.EmployeeNo
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06';
-- delete from WorkRecord2
DELETE w
FROM WorkRecord2 w
INNER JOIN #deletedRecords d
ON w.EmployeeRun = d.employeeId;
-- delete from Employee using exists
DELETE
FROM Employee
WHERE EXISTS (SELECT 1
FROM #deletedRecords d
WHERE d.employeeId = EmployeeNo);
-- drop temporary table
DROP TABLE #deletedRecords;
COMMIT TRANSACTION;
请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=d52c6c1ed91669d68fcc6bc91cb32d78 检查
使用 SELECT INTO 创建临时表的替代方法
BEGIN TRANSACTION;
SELECT e.EmployeeNo employeeId
INTO #deletedRecords
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06';
-- delete from WorkRecord2
DELETE w
FROM WorkRecord2 w
INNER JOIN #deletedRecords d
ON w.EmployeeRun = d.employeeId;
-- delete from Employee using exists
DELETE
FROM Employee
WHERE EXISTS (SELECT 1
FROM #deletedRecords d
WHERE d.employeeId = EmployeeNo);
-- drop temporary table
DROP TABLE #deletedRecords;
COMMIT TRANSACTION;
请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=0f02f05616ce5b4dcc8fc67c6cf1e640 检查
使用 JOIN 删除单个表数据
DELETE w
FROM WorkRecord2 w
INNER JOIN Employee e
ON e.EmployeeNo = w.EmployeeRun
AND w.Company = 1
AND w.date = '2013-05-06'
请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=84a60d1368556a8837281df36579334a 检查
使用 CTE 删除单表数据
WITH cte AS (
SELECT w.EmployeeRun
FROM WorkRecord2 w
WHERE EXISTS (SELECT 1
FROM Employee
WHERE EmployeeNo = w.EmployeeRun)
AND w.Company = 1
AND w.date = '2013-05-06'
)
DELETE
FROM cte
请从网址 https://dbfiddle.uk/?rdbms=sqlserver_2014&fiddle=6590007b3c8c2ffad5563bd86606c5b1 检查
在子表中创建外键时使用 ON CASCADE DELETE。如果删除父表数据,则相应的子表数据将被自动删除。