select排序问题,如何解决问题!!在线等.....
select top10 * from(select top10 a,b,c,d,e from ta where a= 'xxx ',b= 'yyy '
order by a desc,b asc
union
select top10 a,b,c,d,e from ta where a= 'rrrrr ',b= 'mmmm '
order by a desc,b asc
union
select top10 a,b,c,d,e from ta where a= 'llll ',b= 'nnnn '
order by a desc,b asc)as tc
我的意思是想从3个按不同条件选择和排序的语句选出的记录中再一次选出前10条记录(不用排序),如:
where a= 'xxx ',b= 'yyy ' order by a desc,b asc选出2条记录:
记录A
记录B
where a= 'rrrrr ',b= 'mmmm ' order by a desc,b asc选出3条记录:
记录C
记录D
记录E
where a= 'llll ',b= 'nnnn ' order by a desc,b asc选出7条记录:
记录F
记录G
记录H
记录L
记录M
记录N
记录O
则结果我想得到10条记录:
记录A
记录B
记录C
记录D
记录E
记录F
记录G
记录H
记录L
记录M
而按我上面的做法,3个条件选择单独执行都正确,但是select top 10 * from选出的结果中记录的顺序却不是我所想得到的顺序(3个选择的记录加起来,选择其中前10个)却成了:
记录F
记录L
记录A
记录D
记录B
.
.
.
即重新又排了一次顺序
------解决方案--------------------select top10 * from(select top10 a,b,c,d,e, 'A ' as mysort from ta where a= 'xxx ',b= 'yyy '
order by a desc,b asc
union
select top10 a,b,c,d,e, 'B ' from ta where a= 'rrrrr ',b= 'mmmm '
order by a desc,b asc
union
select top10 a,b,c,d,e, 'C ' from ta where a= 'llll ',b= 'nnnn '
order by a desc,b asc)as tc order by mysort
------解决方案--------------------将union 改成union all