查询排序顺序问题

时间:2016-11-07 20:49:07

标签: sql sorting

我有两个问题,我进一步做一个联合以获得不同的结果。目前,它们按名称按字母顺序排序。

查询1:

 Corporate Comp D
 Corporate Comp E

查询2:

 Corporate Comp A
 Corporate Comp B
 Corporate Comp D
 Corporate Comp E
 Corporate Comp G

所以在结合之后,结果是A B D E G.及其按字母顺序排序,但是,我希望它按第一次查询排序,所以基本上我希望订单像

一样工作

最终排序查询

 Corporate Comp D
 Corporate Comp E
 Corporate Comp A
 Corporate Comp B
 Corporate Comp G

3 个答案:

答案 0 :(得分:2)

在这种情况下,请勿使用UNION。这是另一种选择:

select qq.col
from ((select q.col, 1 as which
       from query1 q
      ) union all
      (select q.col, 2 as which
       from query2 q
       where not exists (select 1 from query1 q1 where q1.col = q.col)
      )
     ) qq
order by qq.which, qq.col;

或者,您可以使用聚合:

select qq.col
from ((select q.col, 1 as which
       from query1 q
      ) union all
      (select q.col, 2 as which
       from query2 q
      )
     ) qq
group by qq.col
order by min(qq.which), qq.col;

答案 1 :(得分:0)

你可以试试这个: -

$message = 'Hello. This is some string with 9.15-11.15 time inside, and I don\'t want to replace the DOTS... inside';
$message = preg_replace('/(\d{1,2})\.(\d{1,2})\-(\d{1,2})\.(\d{1,2})/','$1:$2-$3:$4', $message);
var_dump($message);
// string(102) "Hello. This is some string with 9:15-11:15 time inside, and I don't want to replace the DOTS... inside"

答案 2 :(得分:0)

select      col

from       (          select distinct 1 as i,col from query1
            union all (select 2,col from query2 minus select 2,col from query1)
           ) t

 order by   i,col      
 ;