求个SQL!
存在A、B、C表
A表
id
0
1
2
B表
aid name
0 b
2 b
C表
aid name
1 c
得到A表关联B表数据或关联C表数据(判断A id是否存在B表,存在则关联,反关联C表)
得到
id name
0 b
1 c
2 b
------解决方案--------------------select * from A inner join (select * from B left join C on B.id=C.id) as D on A.id=D.id
------解决方案--------------------select A.id,(case when isnull(B.name,'')='' then C.name else B.name end) name from A
left outer join B on A.id=B.aid
left outer join (select * from C where aid not in(select aid from b)) c on A.id=c.aid
order by A.id
给分哦
------解决方案--------------------select A.ID, name= case when b.id is not null then b.name else c.name end from A left join B on A.ID =B.ID
left join C on A.ID = C.ID
------解决方案--------------------select * from B where id exists (select id from A)
union
select * from C where aid exists (select id from A)
这样嘛?
------解决方案--------------------SQL code
select A.ID,CASE WHEN B.name IS NULL THEN C.Name else B.Name end Name from A left join B on A.ID=B.AID
left join C on A.ID=C.AID
------解决方案--------------------
id name
0 B
1 C
2 B
你确定不好使,这是我照你的表创建的,执行SQL后很你显示的结果也一致,你到底需要什么效果,请表达清楚