日期:2014-05-18  浏览次数:20416 次

关于排序问题。
我现在有一个表sub。排序时需要用以下几个字段:ispublic,istop,toptime,subtime
第一是把ispublic=1的数据按subtime降序排在最前面,
第二是把istop=1的数据按toptime降序排在其次。
第三是把剩下的按照subtime降序排列。

select   *   from   sub   where   isnull(ispublic,0)=1   order   by   subtime   desc
union   all
select   *   from   sub   where   isnull(istop,0)=1   and   isnull(ispublic,0) <> 1   order   by   toptime   desc
union   all
select   *   from   sub   where   isnull(istop,0) <> 1     and   isnull(ispublic,0) <> 1   order   by   subtime   desc
按照想法是这样排。不过sql的语法不允许。所以求高手给一答案。

------解决方案--------------------
select * from sub
order by case
when ispublic=1 then 1
when istop=1 then 2
else 3 end,
subtime desc,toptime desc
------解决方案--------------------
如果你加了Order By 再Union All
是需要指定Top的
------------------
Limpire(昨夜小楼)已经给出了正确答案了