select staffid ,projectid from A where projectid=xxx
union 
select staffid ,projectid from B where projectid=xxx

解决方案 »

  1.   

    select staffid ,projectid from table1
    union
    select staffid ,projectid from table2
      

  2.   

    这样联合之后,表1中有的staffid和表2中有的staffid都在一起了,这样就出现了同一个staffid出现了两次的情况,就是说有的staffid在表1中有在表2中也有。
      

  3.   

    你的table1中的staffid和table2中的staffid相同的记录的projectid是不是相同?如果相同使用union可以实现,如果不同,你希望保留哪个表中的projectid?如果都保留同样适用union实现。
      

  4.   

    那通过union是可以实现的
    select staffid ,projectid from table1
    union
    select staffid ,projectid from table2这样得到的结果中不会存在staffid和projectid都相同的记录的
      

  5.   

    如果需要查三列,第三列的列名不一样,字段中的值也不一样,我想保留表1中第三列的值,将表二中的第三列的值设为7,即
    select staffid ,projectid ,staffrole from table1
    union
    select staffid ,projectid,7 from table2
    这时就出现了staffid值相同的情况了。
      

  6.   

    那这样当然会有staffid值相同的记录了,按照这种写法是staffid,projectid,staffrole三个不同时重复的,否则第三个字段就只能保留一个值,要么是表一中内容,要么是7
      

  7.   

    select a.*,b.staffrole from (
    select staffid ,projectid from table1
    union
    select staffid ,projectid from table2)a 
    inner join table1 b on a.staffid=b.staffid and a.projectid=b.projectid
      

  8.   

    不好意思,写习惯了,这是sql server的语法
    oracle应该是
    select a.*,b.staffrole from (
    select staffid ,projectid from table1
    union
    select staffid ,projectid from table2)a,table1 b 
    where a.staffid=b.staffid and a.projectid=b.projectid
      

  9.   

    如果表1为a,表2为b ,那么语句是:select a.*,b.staffrole from (
    select staffid ,projectid from a
    union
    select staffid ,projectid from b)a,table1 b 
    where a.staffid=b.staffid and a.projectid=b.projectid这样对吗?
    但好像不行呀,那个括号后面的a,table1 b 是什么意思呀
      

  10.   

    是把先取出来的一个子查询定义一个别名a,和table1进行连接
    你可以直接把我刚才发的复制过去,不用自己多加a和b的,虽然语句我没有经过测试,不过应该没有错误的
      

  11.   

    这里面总共就有两个表,table1和table2,不过是table1使用了两次罢了
      

  12.   

    我知道的,是这样的,表1和表2都有三个字段,前两个字段的名字都是一样的,第三个不一样,例如,表1
    staffid  serial staffrole
    1782      1042   2
    1786      1042   3
    1852      1042   5
    表2
    staffid  serial  bugrole
    1782      1042    0
    1783      1042    0
    1784      1042    20
    1785      1042    30
    1786      1042    35
    1787      1042    36想把两个表联合在一起,想要的结果是:
    staffid serial   staffrole
    1782     1042    2
    1783     1042    7
    1784     1042    7
    1785     1042    7
    1786     1042    3
    1787     1042    7
    1852     1042    5
      

  13.   

    昨天下班就回去了,没看到
    select a.*,decode(b.staffrole,null,7,b.staffrole) staffrole from (
    select staffid ,projectid from a
    union
    select staffid ,projectid from b)a,table1 b 
    where a.staffid=b.staffid and a.projectid=b.projectid(+)这样应该符合你的要求吧?
    在sql server可以使用case when实现,在oracle中提供了decode函数可以直接实现的
      

  14.   

    select staffid,serial,decode(a.staffrole,null,7,null) as staffrole from 
    (select staffid,serial from a
    union
    select staffid,serial from b)
      

  15.   

    还是不行呀,按照上面的方面只有staffrole是2,3,5那三条数据了。哭死............
      

  16.   

    select staffid,serial,staffrole l3
       from a
       where a.staffid||b.serial in
            (select staffid||serial
                  from a
              intersect
            select staffid||serial
                  from b)
    union
    select staffid,serial,7 l3
       from b
       where a.staffid||b.serial in
            (select staffid||serial
                  from b
              minus
            select staffid||serial
                  from a)
    order by 1