簡體   English   中英

sql比較同一張表中的兩個唯一行

[英]sql compare two unique rows from the same table

我希望這是一個直截了當的問題,但我無法解決它的語法。

我只需要查找“ ID2”中是否還存在“ ID”中的值,可以說名為“ teacher”的表

ID-ID2
10-1
11-2
12-13
13-4

唯一的匹配項是第4行,因為id2中也存在第13行,因此我需要通過選擇查詢將其刪除,有人可以建議嗎? 謝謝。

嗨,除此之外,我還有第二張表,名為“ staff”,具有以下設置

ID-Name
1-smith
2-jones
3-bruce

因此,ID與教師表中的ID是相同的,我想我需要在這里加入它們,但是我不確定第二張表中的ID如何處理。 我需要從第二個表中獲取的唯一信息是名稱,因此僅對表1中的處理進行處理后,笛卡爾乘積應與上面所示類似。

取消,解決它,謝謝

這是你想要的嗎?

select t.*
from teacher t
where exists (select 1 from teacher t2 where t2.id = t.id2);
select *
from teacher
where id in (select distinct id2 from teacher)

要么

select t1.*
from teacher t1
join teacher t2 on t1.id = t2.id2
select t1.* 
from teacher as t1 ,teacher as t2 
where t1.ID = t2.ID2 and t1.ID > t2.ID2 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM