0

我有两张表,相同的结构

  • clients包含第 1+2 行
  • clients3只有第 2 行

我想删除clients表中的第 2 行

 SELECT * 
 FROM clients 
 WHERE EXISTS (SELECT * FROM clients3 WHERE clients3.id = clients.id))

给了我第 2 行。但我不知道如何删除。

DELETE * 
FROM clients 
WHERE (SELECT * 
       FROM clients 
       WHERE EXISTS (SELECT * FROM clients3 WHERE clients3.id = clients.id))

不起作用。

4

3 回答 3

2

无需clients在查询中引用表两次并*DELETE子句中删除 -

DELETE c1
FROM clients c1
INNER JOIN clients3 c3 ON c3.id = c1.id --INNER JOIN will work as EXISTS for 1-1 Relation
于 2012-10-21T12:13:26.860 回答
2

您需要为子查询创建临时表,

DELETE 
FROM clients 
WHERE ID IN 
(
    SELECT ID
    FROM
    (
        SELECT ID FROM clients WHERE EXISTS (SELECT * FROM clients3 WHERE clients3.id = clients.id)
    ) x
)
于 2012-10-21T12:00:29.717 回答
0

尝试这个

   Delete from clients where id  in(select clients3.id from clients3)
于 2017-07-22T20:42:16.800 回答