由于原题比较复杂,有表格
我尽量简单说明下......
--------------------------------------------------------------------------------------------------------------------------------
1)一共四张表:a(fid,orgid...),b(fid,re,xx,yy,...),c(pid1,pid2,re...),d(orgcode,re)2)要查找的有四个字段,分别为:a.fid, c.pid(pid是pid1和pid2 Union联合查询放在一个字段中) a.orgid,d.orgcode3)条件是:①查询出a表中的fid字段                       ②通过fid字段相等在b表中查找re值,并且要求xx='1',yy='2',再通过re值相等查找到c表中的pid1和pid2                      ③查询出a表中的orgid字段                      ④通过fid字段相等在b表中查找re值,并且要求xx='3',yy='4',再通过re值相等查找到d表中的orgcode--------------------------------------------------------------------------------------------------------------------------------
难点1:pid1和pid2查询后怎么合并起来
难点2:②和④查询字段的时候,where条件中re是不同的值,怎么弄的

解决方案 »

  1.   

    --try
    select a.fid,cc.pid,a.orgid,d.ogcode
    from a t,b,(select pid1 as pid re... from c unoin select pid2 pidre... from c) as cc,d
    where t1.fid=b.fid and b.re=cc.re and b.xx='1' and b.yy='2'
      and d.re in (select re from b where fid=t.fid and xx='3' and yy='4')
      

  2.   

    先根据条件查出b,c,d关联的集合,再与a表关联select a.fid,t.pid,a.orgid,t.orgcode
    from a,(
             select b.fid,b.re,ltrim(c.pid1)+ltrim(c.pid2) as c.pid,d.orgcode
             from b
             left join c on b.re=c.re and b.xx='1' and b.yy='2'
             left join d on b.re=d.re and b.xx='3' and b.yy='4'
            ) t
    where a.fid=t.fid
      

  3.   

    select a.fid , c.pid1 , c.pid2 , a.orgid , d.orgcode
    from a , b , c , d
    where a.fid = b.fid and ((b.xx = '1' and b.yy = '2' and b.re = c.re) or (b.xx = '3' and b.yy = '4' and b.re = d.re))--c.pid(pid是pid1和pid2 Union联合查询放在一个字段中)
    select a.fid , c.pid1 + c.pid2 c_pid, a.orgid , d.orgcode
    from a , b , c , d
    where a.fid = b.fid and ((b.xx = '1' and b.yy = '2' and b.re = c.re) or (b.xx = '3' and b.yy = '4' and b.re = d.re))