如何使用SQL Server中的INNER JOIN从多个表中删除
在MySQL中,您可以使用语法
DELETE t1,t2 FROM table1 AS t1 INNER JOIN table2 t2 ... INNER JOIN table3 t3 ...
我如何在SQL Server中做同样的事情?
在这个例子中你可以利用“已删除”伪表。 就像是:
begin transaction; declare @deletedIds table ( id int ); delete t1 output deleted.id into @deletedIds from table1 t1 join table2 t2 on t2.id = t1.id join table3 t3 on t3.id = t2.id; delete t2 from table2 t2 join @deletedIds d on d.id = t2.id; delete t3 from table3 t3 ... commit transaction;
显然你可以做一个“输出删除”。 在第二次删除,如果你需要的东西join第三张表。
另外,还可以在插入语句中插入。*,并在更新语句中插入。*和deleted。*。
编辑:另外,你有没有考虑在table1上添加一个触发器从table2 + 3删除? 您将在隐式事务中,并且也将具有可用的“已插入”和“已删除 ”伪表。
-
您始终可以设置表关系上的级联删除。
-
您可以将多个删除封装在一个存储过程中。
-
您可以使用交易来确保一个工作单元。
从主表中删除一些logging和从两个详细表中删除相应logging的示例:
BEGIN TRAN -- create temporary table for deleted IDs CREATE TABLE #DeleteIds ( Id INT NOT NULL PRIMARY KEY ) -- save IDs of master table records (you want to delete) to temporary table INSERT INTO #DeleteIds(Id) SELECT DISTINCT mt.MasterTableId FROM MasterTable mt INNER JOIN ... WHERE ... -- delete from first detail table using join syntax DELETE d FROM DetailTable_1 D INNER JOIN #DeleteIds X ON D.MasterTableId = X.Id -- delete from second detail table using IN clause DELETE FROM DetailTable_2 WHERE MasterTableId IN ( SELECT X.Id FROM #DeleteIds X ) -- and finally delete from master table DELETE d FROM MasterTable D INNER JOIN #DeleteIds X ON D.MasterTableId = X.Id -- do not forget to drop the temp table DROP TABLE #DeleteIds COMMIT
您可以在SQL Server的DELETE中的FROM子句中使用JOIN语法,但是您仍然只从第一个表中删除,并且它是专有的Transact-SQL扩展,它是子查询的替代方法。
从这里例子:
-- Transact-SQL extension DELETE FROM Sales.SalesPersonQuotaHistory FROM Sales.SalesPersonQuotaHistory AS spqh INNER JOIN Sales.SalesPerson AS sp ON spqh.BusinessEntityID = sp.BusinessEntityID WHERE sp.SalesYTD > 2500000.00;
只是想知道..是真的可能在MySQL? 它会删除t1和t2? 或者我只是误解了这个问题。
但是,如果你只是想删除多个连接条件的table1,只是不要别名你想要删除的表
这个:
DELETE t1,t2 FROM table1 AS t1 INNER JOIN table2 t2 ... INNER JOIN table3 t3 ...
应该像这样写在MSSQL中工作:
DELETE table1 FROM table1 INNER JOIN table2 t2 ... INNER JOIN table3 t3 ...
以比较另外两种常见的RDBMS如何执行删除操作:
http://mssql-to-postgresql.blogspot.com/2007/12/deleting-duplicates-in-postgresql-ms.html
基本上,没有必要在交易中做出三个删除语句,首先是孩子,然后是父母。 设置级联删除是一个好主意,如果这不是一次性的,它的存在不会与任何现有的触发器设置冲突。
在SQL服务器中,无法使用连接删除多个表。 所以你必须在删除表单父项之前先从子项删除。
这是在不离开孤儿的情况下删除logging的另一种方法。
声明@user表(keyValue int,someString varchar(10)) 插入@user 值(1,'1值') 插入@user 值(2,'2值') 插入@user 值(3,'3值') 声明@password表(keyValue int,details varchar(10)) 插入@password 值(1,'1密码') 插入@password 值(2,'2密码') 插入@password 值(3,'3密码') - 在删除之前 从@passwordselect*内部连接@user b 在a.keyvalue = b.keyvalue 从@user中select* into #deletedID其中keyvalue = 1 - 这与输出示例类似 删除@user其中keyvalue = 1 删除@password其中的keyvalue(从#deletedid中selectkeyvalue) - 删除后 - 从@passwordselect*内部连接@user b 在a.keyvalue = b.keyvalue
所有被指出。 只要在父table
上使用DELETE ON CASCADE
,或者从child-table
删除父table
。
正如Aaron已经指出的那样,您可以将删除行为设置为CASCADE,并在父logging被删除时删除子logging。 除非你想要发生某种其他的魔法(在这种情况下,Aaron的答复的第2,3点是有用的),我不明白你为什么需要用内部连接删除。
build立在约翰·吉布(John Gibb)的回答之上,用两个FK关系删除一组数据:
--*** To delete from tblMain which JOINs to (has a FK of) tblReferredTo's PK -- ie ON tblMain.Refer_FK = tblReferredTo.ID --*** !!! If you're CERTAIN that no other rows anywhere also refer to the -- specific rows in tblReferredTo !!! BEGIN TRAN; --*** Keep the ID's from tblReferredTo when we DELETE from tblMain DECLARE @tblDeletedRefs TABLE ( ID INT ); --*** DELETE from the referring table first DELETE FROM tblMain OUTPUT DELETED.Refer_FK INTO @tblDeletedRefs -- doesn't matter that this isn't DISTINCT, the following DELETE still works. WHERE ..... -- be careful if filtering, what if other rows -- in tblMain (or elsewhere) also point to the tblReferredTo rows? --*** Now we can remove the referred to rows, even though tblMain no longer refers to them. DELETE tblReferredTo FROM tblReferredTo INNER JOIN @tblDeletedRefs Removed ON tblReferredTo.ID = Removed.ID; COMMIT TRAN;
DELETE TABLE1 LIN FROM TABLE1 LIN INNER JOIN TABLE2 LCS ON CONDITION WHERE CONDITION
$ sql =“DELETE FROM basic_tbl
, education_tbl
, personal_tbl
, address_tbl
, department_tbl
USING basic_tbl
, education_tbl
, personal_tbl
, address_tbl
, department_tbl
WHERE b_id
= e_id
= p_id
= a_id
= d_id
='”。$ id。 $ RS = mysqli_query($ CON,$ SQL);