首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

关于排序有关问题

2012-01-05 
关于排序问题。我现在有一个表sub。排序时需要用以下几个字段:ispublic,istop,toptime,subtime第一是把ispub

关于排序问题。
我现在有一个表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(昨夜小楼)已经给出了正确答案了

热点排行
Bad Request.