A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孙琦 黑马帝   /  2012-1-24 16:39  /  2661 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

sql中union all 的问题
数据库:access

表数据new:

id  newsid  newsTitle    newsclass  updateTime

1     1    第一条新闻      a1         2008-5-23 12:00:00
2     2    第一条新闻      a1         2008-5-24 12:00:00
3     3    第一条新闻      a2         2008-5-13 12:00:00
4     4    第一条新闻      a1         2008-5-25 12:00:00
5     5    第一条新闻      a2         2008-5-23 12:00:00
6     6    第一条新闻      a2         2008-5-28 12:00:00

……

想按照newsclass分类,然后按照时间排序,取前边10条,sql语句如下:

select top 10  * from [new]  where newsclass='a1' order by updateTime desc union all select top 10  * from [new]  where newsclass='a2' order by updateTime desc

结果全部都是按照时间排序在显示。。。

为什么呢?怎么才可以得到我要的结果。

1 个回复

倒序浏览
本帖最后由 李过 于 2012-7-26 13:37 编辑

select * from
(select top 10  * from [new]  where newsclass='a1' order by updateTime desc union all select top 10  * from [new]  where newsclass='a2' order by updateTime desc)
as temp
order by newsclass, updateTime desc;


是这样吗?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马