UNION查询 - 显示订单查询的结果

时间:2010-11-02 08:41:11

标签: sql sql-order-by union union-all

示例 -

select * from discussion where title like '%india%' 
UNION 
select * from discussion where title like '%Australia%'

它按照讨论ID的顺序显示结果两种类型的结果

我想首先显示印度的结果然后显示澳大利亚的结果,我不能使用Option ALl,因为我还需要删除重复的行。

应该做什么?

3 个答案:

答案 0 :(得分:5)

您可以在

上添加要订购的列
select *, 1 as ORD from discussion where title like '%india%' 
UNION 
select *, 2 as ORD from discussion where title like '%Australia%'

order by ORD

编辑 - 2010年11月29日

由于与ORD问题的重复,我正在考虑一种,或许,更优雅的方式来实现这个

Select * from discussion
where title like '%india%' or title like '%Australia%'
order by (case when title like '%india%'then 1 else 2 end)

答案 1 :(得分:1)

尝试:

SELECT * FROM
(
  select 1 OrderNo, d.* from discussion d where title like '%india%' 
  UNION 
  select 2 OrderNo, d.* from discussion d where title like '%Australia%'
)
ORder by OrderNo

答案 2 :(得分:0)

select * from
(
select * from discussion where title like '%india%' 
UNION 
select * from discussion where title like '%Australia%'
)
ORDER BY title DESC
;