删除条件记录

时间:2011-11-16 14:14:26

标签: sql sql-server sql-server-2000

我正在使用SQL Server 2000

表1

ID  Date

001 23/02/2009
001 24/02/2009
002 25/02/2009
....

表2

ID  Date

001 23/02/2009
002 25/02/2009

一样查询
Delete from table1 where table1.id = table2.id and table1.date = table2.date

如何查询上述条件

6 个答案:

答案 0 :(得分:3)

delete table1
from table2
where table1.id = table2.id and 
      table1.date = table2.date

delete table1使table1成为删除目标。您可以写delete from table1,但from是可选的。

from table2指定删除的来源。使用“second”from子句是一个t-sql扩展,用于匹配相应的行。

最后,where子句连接列iddate上的表,导致table1中的匹配行被删除。

where table1.id = table2.id and 
      table1.date = table2.date

答案 1 :(得分:1)

我会做


DELETE FROM TABLE1 
WHERE ID IN (SELECT t1.ID 
              FROM TABLE1 t1 
                 INNER JOIN TABLE2 t2 ON t1.ID = t2.ID AND t1.Date = t2.Date)

这样,您可以单独运行select语句以查看它将要删除的数据

答案 2 :(得分:1)

使用exists通常更好(更快):

delete t1
from table1 t1
where exists (select 1 from table2 t2 where t1.id = t2.id and t1.date = t2.date)

答案 3 :(得分:0)

您也可以将这些表连接在一起进行删除:

DELETE FROM t1
FROM Table1 AS t1
INNER JOIN Table2 AS t2 ON t1.id = t2.id
   AND t1.Date = t2.Date

答案 4 :(得分:0)

可以通过以下方式完成:

一个。使用加入操作

DELETE FROM Table1
FROM Table1 INNER JOIN
Table2 ON Table1.Date = Table2.Date AND Table1.Id = Table2.ID

湾使用WHERE子句

DELETE TABLE1
FROM TABLE2
WHERE Table1.Date = Table2.Date AND Table1.Id = Table2.ID

答案 5 :(得分:-1)

DELETE FROM table1
WHERE
( SELECT table1.id, table2.id 
FROM table1, table2 
WHERE table1.id = table2.id and table1.date = table2.date )
相关问题