日期:2014-05-16  浏览次数:20464 次

Oracle 联合主键查询数据问题
serviceaction 主键(orderno,actionno)其它表主键就是一个。
select sp.orderno,sp.deptno,sp.zb_ordertime,
       sp.consumer,sp.computertypeno,sp.servicetype,
       sp.computerid,sp.product_code,sp.system,
       op.orderprogress,sa.updatetime,sa.actionno,sa.missreason,
       r.reason
from servicerepair sp,serviceaction sa,orderprogress op,reason r
where sp.orderno = sa.orderno 
      and sp.orderprogressno = op.orderprogressno
      and sp.orderprogressno in (-1,0,1,2,3,4)
      and sa.missreason = r.reasonno
      and sp.deptno = 'GZ'
      and (sa.orderno,sa.actionno) in (
           select s.orderno,max(s.actionno) 
           from (select sp.orderno,sa.actionno
                 from servicerepair sp,serviceaction sa
                 where sp.orderno = sa.orderno 
                 and sp.orderprogressno in (-1,0,1,2,3,4)
                 and sp.deptno = 'GZ') s
           group by s.orderno
           );

纠结我1个多小时才写出来,因为serviceaction表里的同一个ordeno会有多个actionno与其对应。。。(使用group by解决)