Recommended SQL Method for randomly extracting several pieces of data

Source: Internet
Author: User

It is said that this statement works: select top 5 * from tablename order by newid ()
I put it in the SQL query analyzer to execute it. It really works. I randomly extract 5 pieces of information and keep changing it. As a result, I don't need to use them when I use them in the program. They are always those pieces, so I made a research on this thing to find another method:
Newid () can generate data results such as "49869293-8891-4B31-B88E-A584D9621490", and each row produces different results. In this case, we can add such a column to the result set during the query, for example:
Select *, newsid () from Table Name
Then we are sorting this result set, because each newid () produces different values, so if it is sorted, of course it is also different, in this way, we can randomly select a few pieces of data. Then we add a top statement from the front to become:
Copy codeThe Code is as follows:
Select top 10 *, NewID () as random from table name order by random

You can use this method. We want the result. If you don't believe it, you can try it.

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.