How do I randomly select n records or randomly sort records? _ PHP Tutorial

Source: Internet
Author: User
How do I randomly select n records or randomly sort records ?. Q. How do I obtain random sorting results? A. You can use random numbers to obtain columns in random sorting or return x randomly selected columns. However, the RAND function returns only one Q in a query. how do I obtain the random sorting result?
A. You can use random numbers to obtain columns in random sorting or return x randomly selected columns. However, the RAND function can return only one result in a query. You can create order by on the columns returned BY the NOWID function. See the example:
SELECT *
FROM Northwind... Orders
Order by newid ()
Select top 10 *
FROM Northwind... Orders
Order by newid ()
This article is really difficult to translate. it is simply a free translation of the original text.
However, it should be noted that this method scans the entire table and generates a computed column for sorting. it is best not to perform such operations on large tables, otherwise it will be 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 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. how to get random sorting results? A. You can use random numbers to obtain columns in random sorting or return x randomly selected columns. However, the RAND function can return only one query...

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.