union排序的问题
在一个视图里用了union 在首页显示前几条出来 每个分类都按照最新的时间排序
如下:
select top 10 id,title,time,kid from News where kid = 1 ORDER BY time DESC
UNION
select top 6 id,title,time,kid from News where kid = 5 ORDER BY time DESC
UNION
select top 8 id,title,time,kid from News where kid = 3 ORDER BY time DESC
UNION
select top 8 id,title,time,kid from News where kid = 7 ORDER BY time DESC
UNION
select top 6 id,title,time,kid from News where kid = 6 ORDER BY time DESC
UNION
select top 10 id,title,time,kid from News where kid = 2 ORDER BY time DESC
单个语句的话 按最新时间排序是没问题的 可是这6个合起来 就不对了
帮帮忙 谢谢了
------解决方案--------------------建个表变量啊,先都插进去,然后再select order by
------解决方案--------------------也可以外面使用时order by,.net的linq
------解决方案--------------------楼主是想要kid = 1 前10条(按时间降序)后门进跟着kid=5的前6条(按时间降序)……?这样显示吗?
------解决方案--------------------在这个SQL语句的外面套一个select * from 你的union集合 order by time?desc
------解决方案--------------------能实现吗?
------解决方案--------------------select * from (
select top 10 id,title,time,kid from News where kid = 1
UNION
select top 6 id,title,time,kid from News where kid = 5
UNION
select top 8 id,title,time,kid from News where kid = 3
UNION
select top 8 id,title,time,kid from News where kid = 7
UNION
select top 6 id,title,time,kid from News where kid = 6
UNION
select top 10 id,title,time,kid from News where kid = 2 ) ORDER BY time DESC
你是相对整体排序吧
------解决方案--------------------
你需要再对union之后的查询结果在排序。
select b.* from
(
select top 10 id,title,time,kid from News where kid = 1 ORDER BY time DESC
UNION
select top 6 id,title,time,kid from News where kid = 5 ORDER BY time DESC
UNION
select top 8 id,title,time,kid from News where kid = 3 ORDER BY time DESC
UNION
select top 8 id,title,time,kid from News where kid = 7 ORDER BY time DESC