黑马程序员技术交流社区

标题: SQL中UNOIN ALL问题 [打印本页]

作者: 孙琦    时间: 2012-1-24 16:39
标题: SQL中UNOIN ALL问题
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

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

为什么呢?怎么才可以得到我要的结果。
作者: 黑马李果    时间: 2012-7-26 13:34
本帖最后由 李过 于 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;


是这样吗?




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2