use [yourdatabase] select * into #tb1 from ch_news where times>=DATEADD(day, - 30, GETDATE()) order by hits desc select * into #tb2 from ch_news where times<DATEADD(day, - 30, GETDATE()) order by hits desc select top 10 * from ((select * from #tb1) union all (select * from #tb2))tb drop table #tb1,#tb2 这样应该可以吧!测试下看看!
--楼主可以换个思考方法,就是按照时间倒序排列,取前10条。其实跟你想要的结果是一样的。不过做法就简单的多。
select top 10 * from ch_news order by 时间 desc
--哦,忘记条件了。
select top 10 * from ch_news where 日期 between 时间段1 and 时间段2 order by 时间 desc
表为ch_news,查询的条件为
(times-添加时间,hits-点击率)
1.首先返回10条30天内的记录,根据hits排序
2.如果记录数不足10条,再根据点击率自动补充以达到10条记录.
use [yourdatabase]
select * into #tb1 from ch_news where times>=DATEADD(day, - 30, GETDATE()) order by hits desc
select * into #tb2 from ch_news where times<DATEADD(day, - 30, GETDATE()) order by hits desc
select top 10 * from ((select * from #tb1) union all (select * from #tb2))tb
drop table #tb1,#tb2
这样应该可以吧!测试下看看!