有一个表A,是日志记录表,另一个表是状态表,现在想取得A表所有记录,并取得B表中的名字字段,新建成状态名称。意思就是比如A表中某条有userstid='01',dealstid='05'两个值,在表B中对应着
id          name
01          正常
05          失败最终希望组合成这个样子:
userstid   userstname   dealstid      dealstname
   01        正常          05             失败请问如何实现

解决方案 »

  1.   

    SQL> select * from t_joina;UERSTID              DEALSTID
    -------------------- --------------------
    01                   05SQL> select * from t_joinb;ID                   NAME
    -------------------- --------------------
    01                   正常
    05                   失败SQL> 
    SQL> select a1.uerstid,a2.dealstid,b1.name,b2.name
      2  from (t_joina a1 join t_joina a2
      3       on a1.uerstid=a2.uerstid
      4          and a1.dealstid=a2.dealstid)
      5       left join t_joinb b1 on a1.uerstid=b1.id
      6       left join t_joinb b2 on a2.dealstid=b2.id
      7  ;UERSTID              DEALSTID             NAME                 NAME
    -------------------- -------------------- -------------------- --------------------
    01                   05                   正常                 失败SQL> 上面的语句对A表和B表的数据有一定要求,B表里的ID要求唯一,A表usrstid和dealstid要求唯一。
      

  2.   

    补充一下:A表的N个id,完全可能是同一个人,在下刚才忘记提到。
      

  3.   

    理解错了,A表的ID是唯一的,谢谢
      

  4.   

    A有关键字段吧?select a1.uerstid,a2.dealstid,b1.name,b2.name
    from (t_joina a1 join t_joina a2 
         on a1.A表关键字段=a2.A表关键字段) 
         left join t_joinb b1 on a1.uerstid=b1.id
         left join t_joinb b2 on a2.dealstid=b2.id