-2
我有兩個表,父(#tableA)和兒童(#tableB)最佳SQL查詢來獲取存在於所有家長共同子女
在下面的腳本 給予我需要獲得這只是一個紀錄例如'C3',它是P1,P2和P3的常見孩子。
我只能想到下面的這個方法。
Create table #tableA (PkCol varchar(3))
Create table #tableB (FkCol varchar(3), FKChild varchar(10))
Insert Into #tableA
Select 'P1' union
Select 'P2' union
Select 'P3'
Insert Into #tableB
Select 'P1', 'C1' union
Select 'P1', 'C2' union
Select 'P1', 'C3' union
Select 'P2', 'C3' union
Select 'P2', 'C4' union
Select 'P2', 'C5' union
Select 'P3', 'C3' union
Select 'P3', 'C6' union
Select 'P3', 'C7'
Select tb.FKChild from #tableA ta Inner Join #tableB tb on tb.FkCol = ta.PkCol
Group By tb.FKChild
Having Count(Distinct ta.PkCol) = (Select Count(*) from #tableA)
Drop table #tableA
Drop table #tableB
有沒有更好的方式來做到這一點是乾淨的,當實際父表已經接近1K記錄和子表有說接近200多萬條記錄,不會影響性能?