How to randomly select N records or random order of records? _php Tutorials

Source: Internet
Author: User
Q. How do I get a random sort result?
A. To get a randomly sorted column, or to return a randomly selected column of x, you can use a random number. However, the RAND function can only return one result in a query. You can do an order by on the column returned by the NOWID function. Take a look at the example:
SELECT *
From Northwind. Orders
ORDER by NEWID ()
SELECT TOP 10 *
From Northwind. Orders
ORDER by NEWID ()
This passage is really laborious, regardless of the original text, the direct translation.
However, it is important to note that this method is to scan the entire table, and then produce a computed column reorder, it is best not to do such a large table, otherwise it will be very slow.
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 was resolved only once for the entire query, so every row would get same value. You can use a 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/631149.html techarticle Q. How do I get a random sort result? A. To get a randomly sorted column, or to return a randomly selected column of x, you can use a random number. But the RAND function can only return one in a query ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.