如何比较两个表并根据结果做出反应。例如。 我有2个表(确切结构):tableA(key,text)和tableB(key,text)和一个结果表(key,field,case)。sql - 比较表
if key is in tableA, but not in tableB --> case: insert
if key is in tableB, but not in tableA --> case: delete
if key is in tableA and in tableB, but the text is different -> update
if key is in tableA and in tableB, and the text is the same -> nothing
结果表如下所示:
key | text | case
------------------
1 | t1 | update
2 | t2 | delete
3 | t3 | insert
4 | t4 | nothing
是否有可能只用一个查询办呢?
得到插入(和删除反之亦然):
SELECT key FROM tableA
MINUS
SELECT key FROM tableB;
这是我正在寻找并给我正确的结果。但是,如果我必须比较更多的文本列,那是什么。我是否必须将它们全部添加或是否有机会比较“比较除键之外的所有内容”? – Marc 2013-02-14 10:28:09
除了添加全部内容之外,我现在想不出任何方法。 – 2013-02-14 10:34:17