现在想要实现下面效果,不知道如何实现纠结了很久.....
Table1表如下:
   postId     sup_id   realName  postType
      1         12        Tr        1     
      2         13        jk        1
      3         14        pl        1Table2表如下:
   postId     sup_id   realName  postType
      1         12        Tr        2     
      2         13        gk        2
      3         14        hj        2怎么样通过union把两表的结果链接起来然后实现下面的结果    postId     sup_id   realName  postType
      1         12        Tr        2     
      2         13        jk        1
      3         14        pl        1
      2         13        gk        2
      3         14        hj        2

解决方案 »

  1.   

    1 12 Tr 1 和1 12 Tr 2   为什么变成一条了? 规则是什么?
      

  2.   

    前面三个字段数据都相同就选择Table2数据,Table1中的数据就不需要了
      

  3.   


    if object_id('[Table1]') is not null drop table [Table1]
    go
    create table [Table1] (postId int,sup_id int,realName nvarchar(4),postType int)
    insert into [Table1]
    select 1,12,'Tr',1 union all
    select 2,13,'jk',1 union all
    select 3,14,'pl',1if object_id('[Table2]') is not null drop table [Table2]
    go
    create table [Table2] (postId int,sup_id int,realName nvarchar(4),postType int)
    insert into [Table2]
    select 1,12,'Tr',2 union all
    select 2,13,'gk',2 union all
    select 3,14,'hj',2select * from [Table1]
    select * from [Table2]select postid,sup_id,realname,MAX(posttype)
    from (
    select postid,sup_id,realname,posttype from Table1 
    union 
    select postid,sup_id,realname,posttype from Table2 ) T
    group by postid,sup_id,realname/*
    1 12 Tr 2
    2 13 gk 2
    2 13 jk 1
    3 14 hj 2
    3 14 pl 1*/
      

  4.   

    select *,isnull((select posttype from table2 where postid=a.postid and sup_id=a.sup_id and realname=a.realname),(select posttype from table1 where postid=a.postid and sup_id=a.sup_id and realname=a.realname))
    as posttype from (select postid,sup_id,realname from Table1 
    union 
    select postid,sup_id,realname from Table2)a
      

  5.   

    select * from table1
    where Not Exists(Select 1 from table2
    Where table1.postid = table2.postid
    And table1.sup_id = table2.sup_id
    And table1.realName = table2.realName)
    Union
    select * from table2
      

  6.   

    3楼写的有点靠谱,就是用了Max()之后无法 Order by 这个有点让人烦心... 希望还有高手过来指教,继续等待中....