现有文章表table,结构如下:
[id] [int] IDENTITY(1,1)
[title] 文章标题
[zd_id] 置顶标识,0代表不置顶,大于0标识此标题置顶,置顶顺序按数字大小排列,因此每置顶一篇文章,它的zd_id就比原来最大的zd_id大0.01。
[zd_days] 置顶天数
insert_time] 插入日期,默认是getdate()我想到的处理方式如下:
如果zd_id>0 同时 insert_time+zd_days>现在 进入置顶,然后再根据zd_id大小排序
如果zd_id>0 但 insert_time+zd_days<现在 不置顶
如果zd_id=0 不置顶但SQL语句不会写,请诸位大侠看看!
[id] [int] IDENTITY(1,1)
[title] 文章标题
[zd_id] 置顶标识,0代表不置顶,大于0标识此标题置顶,置顶顺序按数字大小排列,因此每置顶一篇文章,它的zd_id就比原来最大的zd_id大0.01。
[zd_days] 置顶天数
insert_time] 插入日期,默认是getdate()我想到的处理方式如下:
如果zd_id>0 同时 insert_time+zd_days>现在 进入置顶,然后再根据zd_id大小排序
如果zd_id>0 但 insert_time+zd_days<现在 不置顶
如果zd_id=0 不置顶但SQL语句不会写,请诸位大侠看看!
then zd_id desc end
dateadd(dd,zd_days,insert_time)>getdate() then zd_id else -1 end desc
dateadd(dd,zd_days,insert_time)>getdate() then zd_id else -1 end desc
then zd_id end desc
then zd_id end desc
修正!
order by
case when zd_id>0 and DATEDIFF(dd,getdate(),insert_time)>0 THEN 0 ELSE zd_id END
case when zd_id>0 and dateadd(dd,zd_days,insert_time)>getdate() then zd_id
when zd_id>0 and dateadd(dd,zd_days,insert_time)<getdate() then -1
when zd_id=0 then -10 end desc
then zd_id end desc
修正楼上!
then zd_id else insert_time end desc
then zd_id else insert_time end desc
from
(select (case when zd_id>0 and dateadd(day, zd_days, insert_time)>getdate() then zd_id
when zd_id>0 and dateadd(day, zd_days, insert_time)<getdate() then 0
when zd_id=0 then -1
else -2
end) as 排序列, *
from [table]
)
order by 1 desc