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()

 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [SQL Server] 的这些文章也感兴趣:

SQL Server全文索引服务
从Oracle到Sql Server--Sql智能翻译器
删除SqlServer发布与复制订阅数据库
使用 SQL Server 2005 中的 SQLCLR 处理 XML Showplan(1)
使用HTTP访问SQLServer
SQL Server 2005 Service Broker 初探(1)
SQL SERVER应用问题解答13例(三)
解析SQL Server自动编号字段的数据恢复
SQL Server的存储过程调用Com组件
影响SQL server性能的关键三个方面