Order by NEWID () random query

Source: Internet
Author: User

In daily operations, sometimes it may be that some activities to draw out the winners or select some of the list, It is often used to order by Newid () the way to do random number selection, but the possible situation needs to be matched to DISTINCT to choose, this time, such as DISTINCT and Order by Newid () will encounter error message /c7>

"If you have specifiedSELECT DISTINCT, youORDER byThe item must appear in the Pick list.The following example shares one of the simplest approaches. Here the sample database is used Zhang Limin Example of a teacher in the book Chinese North Wind Database,By using the sample database in Chinese, we can get a closer look at the effect of this example.If you want to be in this groupDISTINCTRandom number selection at the beginning of data10PenYou might be able to associate it directly with theSELECT DISTINCTEmployee number,Delivery CityFROM dbo.Order main FileOrder by NEWID ()Of course becauseDISTINCTWithin the most filtered data, there is no newid () It's going to happen.   "if you have specified select Distinct ORDER by The item must appear in the Pick list.   newid () add it in. select inside, so order by There's no problem.   So if you use the words, the effect may be that you will find it with all select

so much for that. , what is the solution to the ?  DISTINCT ( screening) Order by Newid () random numbers sorted into two queries, grouped together,

Select TOP * from- Simulation of a data sheet (SELECT DISTINCT employee number , shipping city from dbo. Order main file ) as fictitious data table order by newid ()

By filtering the subquery first, the use of newid () will be able to make such a function easily

SQL Server:

Select TOP N * from TABLE Order by NewID () view plaincopy to Clipboardprint? Select TOP N * from TABLE Order by NewID ()

Select TOP N * from TABLE the Order by NewID () NewID () function creates a unique value of type uniqueidentifier. The above statement achieves the effect of randomly reading N records from a table.

Access:

Select TOP N * from TABLE Order by Rnd (ID) view plaincopy to Clipboardprint? Select TOP N * from TABLE Order by Rnd (ID)

Select TOP N * from TABLE Order by Rnd (ID) Rnd (ID) where the ID is an AutoNumber field that can be done with any other value, such as using the Name field (UserName)

Select TOP N * from TABLE Order by Rnd (Len (UserName)) view Plaincopy to Clipboardprint? Select TOP N * from TABLE Order by Rnd (Len (UserName))

Select TOP N * from TABLE Order by Rnd (Len (UserName))

Mysql:

Select * from TABLE Order by Rand () Limit ten view plaincopy to Clipboardprint? Select * from TABLE Order by Rand () Limit 10

Select * from TABLE Order by Rand () Limit 10

PostgreSQL:

SELECT * from glxt022 order by random () Limit 5 view plaincopy to Clipboardprint? SELECT * from glxt022 order by random () limit 5

Order by NEWID () random 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.