如何隨機選取n條記錄或者對記錄作隨機排序?_PHP教程

來源:互聯網
上載者:User
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

http://www.bkjia.com/PHPjc/631149.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631149.htmlTechArticleQ. 如何得到隨機排序結果? A. 要得到隨機排序的列,或者返回x條隨機播放的列,你可以使用隨機數。但是RAND函數在一個查詢中只能返回一個...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.