首页 新闻 会员 周边

sql随机查询问题

0
[已解决问题] 解决于 2011-11-10 09:54

select top(20) PaperId,CreationTime  from TB_Paper where QuestionTypeId=1 and PaperDegree ='1'  union all
select top(30) PaperId,CreationTime from TB_Paper where QuestionTypeId=1 and PaperDegree ='2' ORDER BY NEWID()

我想要随机查询出满足的条件  根据NEWID()   可是  这样写会报出

消息 104,级别 16,状态 1,第 1 行
如果该语句包含 UNION、INTERSECT 或 EXCEPT 运算符,则 ORDER BY 项必须出现在选择列表中。

和排序字段的时候是一样的 必须要将排序字段同时查询进去,可是这是个随机函数,没办法同时查询  有什么办法解决吗?

飞翔的鱼的主页 飞翔的鱼 | 初学一级 | 园豆:3
提问于:2010-12-27 09:20
< >
分享
最佳答案
0

select * from (

select top(20) PaperId,CreationTime  from TB_Paper where QuestionTypeId=1 and PaperDegree ='1'  union all 
select top(30) PaperId,CreationTime from TB_Paper where QuestionTypeId=1 and PaperDegree ='2

) order by newid()

Gray Zhang | 专家六级 |园豆:17610 | 2010-12-27 10:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册