注册
 找回密码
 注册
江西广告网
查看: 278|回复: 0
打印 上一主题 下一主题

如何随机选取n条记录或对记录作排序

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2008-12-24 11:59:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
  Q. 如何得到随机排序结果?      A. 要得到随机排序的列,或者返回x条随机选择的列,你可以使用随机数。但是RAND函数在一个查询中只能返回一个结果。你可以在NOWID函数返回的列上做ORDER BY。请看示例:      SELECT *   FROM Northwind..Orders   ORDER BY NEWID()      SELECT TOP 10 *   FROM Northwind..Orders   ORDER BY NEWID()      这段话翻译得真是费劲,干脆不管原文,直接意译了。   不过提醒大家注意,这种方法是要对整个表扫描,然后产生一个计算列再排序的,最好不要对大的表作这样的操作,否则会很慢的。      Q. How can I randomly sort query results?      A. To randomly order rows, or to return x number of randomly chosen rows, you can use the RAND function inside the SELECT statement. But the RAND function is resolved only once for the entire query, so every row will get same value. You can use an ORDER BY clause to sort the rows by the result from the NEWID function, as the following code shows:      SELECT *   FROM Northwind..Orders   ORDER BY NEWID()      SELECT TOP 10 *   FROM Northwind..Orders   ORDER BY NEWID()      —SQL Server MVPs <
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表