程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> SQL SERVER中對查詢結果隨機排序

SQL SERVER中對查詢結果隨機排序

編輯:關於SqlServer
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()
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved