SQL SERVER中对查询结果随机排序

译:SQL SERVER中对查询结果随机排序

Randomly Sorting Query Results
查询结果随机排序



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:

答:对结果记录随机排序,或随机返回X条记录,可以通过在SELECT语句中使用RAND函数来实现。但是RAND函数在查询中只生成一次,因此每一行都将得到相同的值。可以通过在ORDER BY子句中使用NEWID函数来对结果进行排序的方法来实现,代码如下:

SELECT *
FROM Northwind..Orders
ORDER BY NEWID()

SELECT TOP 10 *
FROM Northwind..Orders
ORDER BY NEWID()


作者:Water

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值