[英]Compare tables and select match rows to single row of other table
我有兩個表,它們都包含多個匹配記錄,我只想識別兩個表之間的匹配聯系人,並與單行進行比較,如果不匹配則跳過。
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
也許是這樣的?
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中)時,您想要什么結果,您可能需要對其稍加修改。
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;
這是一個想法,您應該嘗試,bot不能完全回答。
另外,您應該嘗試與此結合:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.