有2张表
A
PID   CID
1     11
1     12
1     13
2     21 
2     22
2     23B
ID
1
2
3
请问如何通过一条SQL语句得到C11
12
13
21
22
23
1
2
3

解决方案 »

  1.   

    select CID from A union all select ID from B
      

  2.   

    select cid from a
    union all
    select id from b
      

  3.   

    Select CID as C from A
    union all
    select ID from B
      

  4.   

    select CID c from A
    union all
    select ID c from B
      

  5.   


    ---------------------------
    select CID from A
    union all--union all(包含重复数据),union(不包含重复数据)
    select ID from B
      

  6.   


    select CID from A
    union all
    select ID as CID from B
      

  7.   


    declare @B table (id int , b varchar(10))
    insert into @B
    select 1,'a' union all
    select 2,'b'
    declare @C table (c varchar(10))
    insert into @C 
    select 'c'select b as f from @B
    union 
    select c as f from @C
    /*
    a
    b
    c
    */
      

  8.   

    汗 也许是我没有说清楚 看下面的例子吧 谢谢楼上的诸位了有2张表 

    PID  CID 
    1    11 
    1    12 
    1    13 
    2    21 
    2    22 
    2    23 
    4    41
    4    42B 
    ID 



    要得到C 11 
    12 
    13 
    21 
    22 
    23 


    3
      

  9.   

    select cid
    from
    (
    select A.cid,'A'+cast(cid as varchar(10)) as orderCol
    from A
    union all
    select B.id,'B'+cast(id as varchar(10)) as orderCol
      from
    )B
    ) AA
    order by orderCol
      

  10.   

    select CID c from A
    union all
    select ID c from B
      

  11.   

    select cid
    from
    (
    select A.cid,'A'+cast(cid as varchar(10)) as orderCol
    from  A
    where A.pid in 
    (select ID from B )
    union all
    select B.id,'B'+cast(id as varchar(10)) as orderCol
    from B
    ) A
    order by orderCol
      

  12.   

    select cid
    from
    (
    select A.cid,'A'+cast(cid as varchar(10)) as orderCol
    from  A
    where A.pid in 
    (select ID from B )
    union all
    select B.id,'B'+cast(id as varchar(10)) as orderCol
    from B
    ) A
    order by orderCol
      

  13.   


    /*
    有2张表 

    PID  CID 
    1    11 
    1    12 
    1    13 
    2    21 
    2    22 
    2    23 B 
    ID 



    请问如何通过一条SQL语句得到 C 11 
    12 
    13 
    21 
    22 
    23 


    3
    */create table #t4 (pid int,cid int)
    create table #t5 (id int)insert into #t4 values ('1','11')
    insert into #t4 values ('1','12')
    insert into #t4 values ('1','13')
    insert into #t4 values ('2','21')
    insert into #t4 values ('2','22')
    insert into #t4 values ('2','23')insert into #t5 values ('1')
    insert into #t5 values ('2')
    insert into #t5 values ('3')select cid from #t4 
    union all
    select id from #t5
    cid         
    ----------- 
    11
    12
    13
    21
    22
    23
    1
    2
    3(所影响的行数为 9 行)
      

  14.   


    就是用UNION ALL来的啊
    上面的没错吧
      

  15.   

    select c.cid
    from (select a.cid cid,b.id id from b b left join a a on a.pid = b.id ) c
    union all
    select distinct c.id 
    from (select a.cid cid,b.id id from b b left join a a on a.pid = b.id ) c
      

  16.   

    select CID from A union all select ID from B
      

  17.   

    C表中的数据源由A和B组合的,具体的组合方式表达不清?