[SQL]使用INNER JOIN删除查询吗? [英] [SQL] DELETE Query with INNER JOIN?
问题描述
我当前有以下查询:
I''m currently have this query:
IF NOT EXISTS(SELECT * FROM Table1 WHERE Value = 1)
BEGIN
DELETE FROM Table2 WHERE Value = 1
END
如果其他表的任何行中都不存在值,是否可以使用INNER JOIN从Table2中删除所有行.因为使用此方法将需要首先从Table2中提取所有值,然后对其进行操作,等等...
查询应为:删除Table2中所有未在Table1中出现的值.
如果不是每次都不存在DELETE值...
Is it possible to use INNER JOIN, to delete all the rows from Table2 if a value doesn''t exist in any of the other table''s rows. Because using this would require extracting all the Values from Table2 first, then manipulating it, etc...
The query should be: DELETE all the values from Table2 which do not appear in Table1.
Instead of, DELETE value if it doesn''t exist every time...
推荐答案
是的,您可以使用INNER JOIN从表中删除行. br/>
Yeah, you can simply delete rows from a table using an INNER JOIN.
DELETE t2 FROM table2 AS t2
INNER JOIN table1 ON table1.Value = t2.Value
WHERE table1.Value = 1
MSDN在删除语句中提及联接:删除语句 [ ^ ]
这是一个完整的示例:使用INNER JOIN删除 [
MSDN mentions joins in delete statements: Delete Statement[^]
Here is a full example: Delete with INNER JOIN[^]
Hope that helps :)
delete table2 where value not in
(select value from table1)
希望对您有帮助
Hope it helps
尝试
DELETE FROM table1
WHERE NOT EXISTS
( select value
from table2 where value = 1 )
警告:我自己从未运行过类似的查询.
Warning: I have never run a similar query myself.
这篇关于[SQL]使用INNER JOIN删除查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!