1

例子 -

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

它按讨论 ID 的顺序显示结果,混合了两种类型的结果

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

应该做什么?

4

3 回答 3

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)
于 2010-11-02T08:44:04.913 回答
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
于 2010-11-02T08:43:22.963 回答
0
select * from
(
select * from discussion where title like '%india%' 
UNION 
select * from discussion where title like '%Australia%'
)
ORDER BY title DESC
;
于 2010-11-02T08:45:43.400 回答