22,209
社区成员
发帖
与我相关
我的任务
分享
ID Name
19 sss
20 vvv
ID b1 b2
19 A1 A11
19 B1 B11
20 E1 E11
ID c1 c2
19 C1 C11
20 D1 D11
20 W1 W11
TA.ID TB.b1 TB.b2 TC.c1 Tc.c2
19 A1 A11 C1 C11
19 B2 B11
20 E1 E11 D1 D11
20 W1 W11
TA.ID TA.Name TB.b1 TB.b2 TC.c1 Tc.c2
19 sss A1 A11 C1 C11
19 sss B2 B11
20 vvv E1 E11 D1 D11
20 vvv W1 W11
select isnull(b.id,c.id) as id
,b.b1
,b.b2
,c.c1
,c.c2
from tb b full join
tc c
on b.id=c.id
and (select count(*) from tb where id=b.id and (b1<b.b1 or b1=b.b1 and b2<=b.b2))=
(select count(*) from tc where id=c.id and (c1<c.c1 or c1=c.c1 and c2<=c.c2))
select isnull(t1.id,t2.id) as id
,isnull(t1.name,t2.name) as name
,t1.b1
,t1.b2
,t2.c1
,t2.c2
from (
select a.id,a.name,b.b1,b.b2,
(select count(*) from tb where id=b.id and (b1<b.b1 or b1=b.b1 and b2<=b.b2)) as id1
from ta a,tb b
where a.id=b.id
) as t1 full join
(
select a.id,a.name,b.c1,b.c2,
(select count(*) from tc where id=c.id and (c1<c.c1 or c1=c.c1 and c2<=c.c2)) as id1
from ta a,tc c
where a.id=c.id
) as t2
on t1.id=t2.id
and t1.id1=t2.id1