您好我有2个表具有相同的列名(Id,FName,LName,DOB,Type,Contact,Add)。
现在我希望将所有数据作为一个单独的行从两个表中按照Fname的顺序获得相同的FName,LName和DOB。
我尝试使用交叉和联盟但不适合我。
Select * from #t1 where FirstName in (select FirstName from #t2 ) and LastName in (select LastName from #t2 ) and DateOfBirth in (select DateOfBirth from #t2 )
UNION
Select * from #t2 where FirstName in (select FirstName from #t1 ) and LastName in (select LastName from #t1 ) and DateOfBirth in (select DateOfBirth from #t1 ) order by FirstName ,LastName
由于
答案 0 :(得分:2)
我会这样做,但我确信这是一种更简洁的方式...
SELECT T1.*
FROM T1
INNER JOIN T2
ON T2.FirstName = T1.FirstName
AND T2.LastName = T1.LastName
AND T2.DateOfBirth = T1.DateOfBirth
UNION ALL
SELECT T2.*
FROM T2
INNER JOIN T1
ON T1.FirstName = T2.FirstName
AND T1.LastName = T2.LastName
AND T1.DateOfBirth = T2.DateOfBirth
答案 1 :(得分:1)
这将起作用
Select a.*,b.* from #t1 a join #t2 b on (a.FName=b.FName and a.LName=b.LName and a.DOB=b.DOB)
答案 2 :(得分:1)
试试这个
SELECT T1.Id,
T1.FName,
T1.LName,
T1.DOB,
T1.Type,
T1.Contact,
T1.Add
FROM #t1 T1 INNER JOIN #t2 T2
ON T1.FName=T2.FName
AND T1.LName=T2.LName
AND T1.dob=T2.dob
答案 3 :(得分:0)
另一种解决方法是在union
之后进行汇总。然后检查值是否显示必需的次数:
select firstname, lastname, dob
from ((select firstname, lastname, dob, 't1' as which
from #t1
) union all
(select firstname, lastname, dob, 't2' as which
from #t2
)
) t
group by firstname, lastname, dob
having count(distinct which) = 2;