最近,我在SQL中遇到了查询问题。问题如下: 我有一张叫做“老”的2张桌子。 “新”。两者都具有相同的结构和列“key1”,“key2”和“description”。此外,表“new”包含来自“old”和其他条目的条目的子集。
现在我想从“旧”表中删除所有未出现在“new”中的条目。问题是,相反,“key1”和“key2”是唯一的,只有两者的结合。
示例:
旧:
key1, key2, description
1, 2, "Hello"
1, 3, "World"
2, 3, "!"
新:
key1, key2, description
1, 3, "World"
2, 3, "!"
我尝试了以下sql语句,但它不起作用(至少在Access97中)
DELETE
FROM old
WHERE old.key1<>new.key1 AND old.key2 <> new.key2
有没有人知道如何删除old中的所有条目,这些条目在key1和key2的组合是唯一的约束下不会出现在“new”中?
答案 0 :(得分:3)
DELETE FROM old
WHERE NOT EXISTS (SELECT 1
FROM new
WHERE old.key1 = new.key1
AND old.key2 = new.key2)
您还可以检查描述匹配
DELETE FROM old
WHERE NOT EXISTS (SELECT 1
FROM new
WHERE old.key1 = new.key1
AND old.key2 = new.key2
AND old.description = new.description)
答案 1 :(得分:1)
DELETE *
FROM old
WHERE old.key1 & " " & old.key2 IN (select new.key1 & " " & new.key2 from new )