tb1Name Phone
张三 12345
李四 24687
王五 98765
王五 12345查询结果
Name Phone
张三 12345
李四 24687
王五 98765
王五 12345
张三 NULL
李四 NULL
王五 NULL

解决方案 »

  1.   


    declare @tb1 table (Name varchar(4),Phone int)
    insert into @tb1
    select '张三',12345 union all
    select '李四',24687 union all
    select '王五',98765 union all
    select '王五',12345select * from @tb1
    union all
    select Name,null from @tb1
    group by Name
    /*
    Name Phone
    ---- -----------
    张三   12345
    李四   24687
    王五   98765
    王五   12345
    李四   NULL
    王五   NULL
    张三   NULL
    */
      

  2.   

    select * from tb1
    union all
    select Name,null from tb1
      where name in(select name from tb1 group by name having count(1)=1)
      

  3.   

    select * from tb1
    union all
    select distinct[name],null from tb1
      

  4.   

    select Name,Phone from tb1
    union all
    select distinct[name],null from tb1
      

  5.   

    select * from tb1
    union all
    select distinct [name],null from tb1