开发者

UNION Query-display results in order queries are written

example -

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

It shows me results in order of discussion IDs mixing both typse of results

I want to display India results first then Australia's results and I cant use Op开发者_运维百科tion ALl as I need to remove duplicate rows also.

What should be done?


You could add a column to order on

select *, 1 as ORD from discussion where title like '%india%' 
UNION 
select *, 2 as ORD from discussion where title like '%Australia%'

order by ORD

EDIT - 29/11/2010

Due to the duplicate with ORD problem i was thinking about a, maybe, more elegant way to achive this

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


Try:

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜