日期:2014-05-18  浏览次数:20430 次

创建复杂的查询
现在有3张表,A表有字段AA、DD,B表有字段BB、XX,C表有字段CC、YY,A表字段DD有可能是B表的BB或者C表的CC,要做一个查询包括A表的AA、DD,第三个字段根据DD显示XX或者YY,请问要怎么查呢?

------解决方案--------------------

select a.*.b.xx as xy
from a
join b on b.bb = a.dd
union all
select a.*,c.yy as xy
from a
join c on c.cc = a.dd
------解决方案--------------------
select A.*,B.XX from A,B where A.DD=B.BB
union all
select A.*,C.YY from A,C where A.DD=C.CC
------解决方案--------------------
感覺是這樣

Select
A.AA,
A.DD,
IsNull(B.XX, C.YY) As XXYY
From
A
Left Join
B
On A.DD = B.BB
Left Join
C
On A.DD = C.CC
------解决方案--------------------
现在有3张表,A表有字段AA、DD,B表有字段BB、XX,C表有字段CC、YY,A表字段DD有可能是B表的BB或者C表的CC,要做一个查询包括A表的AA、DD,第三个字段根据DD显示XX或者YY,请问要怎么查呢?

select a.aa,b.xx dd from a,b where a.dd = b.bb
union all
select a.aa,c.yy dd from a,c where a.dd = b.cc
------解决方案--------------------
select a.aa,b.xx dd from a,b where a.dd = b.bb
union all
select a.aa,c.yy dd from a,c where a.dd = c.cc

dawugui(潇洒老乌龟),打錯了吧