a    x
b    y
a    y
a    x
b    z
c    y二字段不重复合并成一个字段输出
a
b
c
x
y
z
请大家帮忙呀!!!!

解决方案 »

  1.   

    select distinct 字段1 from 表 union select distinct 字段2 from 表
      

  2.   


    create table #T(  l1 char(1) ,l2 char(1))
    insert into #T select 'a','x'
    insert into #T select 'b','y'
    insert into #T select 'a','y'
    insert into #T select 'a','x'
    insert into #T select 'b','z'
    insert into #T select 'c','y'
    select  l1 from #T
    union 
    select  l2 from #T
    /*
    l1   
    ---- 
    a
    b
    c
    x
    y
    z(所影响的行数为 6 行)
    */
      

  3.   


    --借用下数据create table #T(  l1 char(1) ,l2 char(1))
    insert into #T select 'a','x'
    insert into #T select 'b','y'
    insert into #T select 'a','y'
    insert into #T select 'a','x'
    insert into #T select 'b','z'
    insert into #T select 'c','y'
    select distinct l1 from #T
    union all
    select distinct l2 from #T
      

  4.   

    select distinct 字段1 from 表 union select distinct 字段2 from 表
      

  5.   

    用union可以但是union all不可以
    union会删除重复数据,union all不会,他会完整的显示全部的数据
      

  6.   

    谢谢各位,还有个问题,问完结贴
    要加限制条件怎么加?
    select distinct 字段1 from 表 union select distinct 字段2 from 表 where 字段3='XXX'这样加查不到正确答案,还望教我!!!!!1  a    x 
    1  b    y 
    1  a    y 
    1  a    x 
    1  b    z 
    1  c    y
    2  a    x 
    2  b    y 
    2  a    y 
    2  a    x 
    2  b    z 
    2  c    y
      

  7.   

    第一个问题。直接union就可以了,本身就是去重复的。第二个问题。select * from
      (select 字段2 from tb
       union
       select 字段3 from tb
      ) a
      where 字段2='xxx'
      

  8.   

    1  a    x 
    1  b    y 
    1  a    y 
    1  a    x 
    1  b    z 
    1  c    y 
    2  e    11 
    2  f    22 
    2  e    22 
    2  e    11 
    2  f    33 
    2  g    22输出:
    e
    f
    g
    11
    22
    33
      

  9.   


    比如我
    条件为:字段1=1
    得到:
    a
    b
    c
    x
    y
    z条件为:字段1=2
    得到:



    11 
    22 
    33
      

  10.   

    select 字段2 from tb where 字段1='1'
    union
    select 字段3 from tb where 字段1='1'--或者select 字段2
    from
    (
    select 字段1,字段2 from tb
    union
    select 字段1,字段3 from tb
    ) x
    where 字段1='1'
      

  11.   

    Select cola from ta where colb='1'
    union 
    select cola as cola from tb where colb='2'