我有两个表,它们都包含多个匹配的记录,我想只识别两个表之间的匹配联系人,并与单行进行比较,如果不匹配则跳过。
table A table B
name |amount name|amount
A |20 C | 50
B |20 A | 20
C |50 A | 20
A |20 B | 50
B |20 B | 20
c |20 C | 20
C | 50
所以我想编写一个SQL查询来得到像B 50这样的结果,C 50不匹配所以我可以突出显示它们
table A table B
name |amount name|amount
A |20 A | 20
A |20 A | 20
B |20 A | 20
B |20 B | 20
C |20 B | 20
C |20 C | 20
C | 20
C | 20
C | 20
所以我想要从表B返回的结果,因为这些条目不匹配。
A 20
C 20
C 20
答案 0 :(得分:0)
也许是这样的?
select a2.name, a2.amount
from ( select name, amount, count(*) as n from A group by name, amount ) a2
join ( select name, amount, count(*) as n from B group by name, amount ) b2 on b2.name = a2.name and b2.amount = a2.amount
where b2.n <> a2.n
由于当A中存在一对名字/金额而不是B(或B中而不是A中)时,我不知道你想要什么结果,你可能需要稍微调整一下。
答案 1 :(得分:0)
SELECT c.name, c.amount, o.name, o.amount
FROM tableA AS c, TableB AS o
WHERE c.name=o.amount AND c.amount=o.amount;
这是一种方式的想法,你应该尝试,机器人没有完成答案。
你也应该尝试与此结合:
SELECT c.name, c.amount, o.name, o.amount, Count(*) as Duplicates
FROM tableA AS c, TableB AS o
WHERE c.name=o.amount AND c.amount=o.amount
having Duplicates > 1;