在合并结果集的时候出现了一些问题,请各位高手帮忙解决一下cp表的内容如下 id    names
1      AA
2      BB
3      CC
4      DD
5      AB
6      AC现在想将两条查询语句查询出来的结果集合并在一起,查询语句如下
select * from cp where names like '%A%' order by id desc
select * from cp order by id desc想要的结果如下
 id    names
6      AC
5      AB
1      AA
6      AC
5      AB
4      DD
3      CC
2      BB
1      AA请问要怎么做啊

解决方案 »

  1.   


    select * from cp where names like '%A%' order by id desc
    UNION ALL
    select * from cp order by id desc 
      

  2.   

    select * from cp where names like '%A%' order by id desc
    UNION ALL
    select * from cp order by id desc 
      

  3.   

    create table a(id int,   names varchar(10))
    insert into a values(1 ,     'AA') 
    insert into a values(2 ,     'BB') 
    insert into a values(3 ,     'CC') 
    insert into a values(4 ,     'DD') 
    insert into a values(5 ,     'AB') 
    insert into a values(6 ,     'AC') select id , names from
    (
      select * , px = 1 from a where names like '%A%' 
      union all
      select * , px = 2 from a
    ) t
    order by px , id desc 
    drop table a/*
    id          names      
    ----------- ---------- 
    6           AC
    5           AB
    1           AA
    6           AC
    5           AB
    4           DD
    3           CC
    2           BB
    1           AA(所影响的行数为 9 行)
    */
      

  4.   

    自己做一个字段select *, 1 as cnt  from cp where names like '%A%' 
    union all
    select * ,0  as cnt from cp
    order by cnt desc, id desc 
      

  5.   

    select * , px = 1 from a where names like '%A%' 
    union all
    select * , px = 2 from a
    order by px , id desc