Get the App
SLTechnology News&Howtos  ›  Database  › 

Order by newid () what is the method of random query in various databases

Shulou Source: shulou.com Published: 2022-05-31 12:52:55 10月01日 Update

Today, I will talk to you about the method of random query of various databases by order by newid(). Many people may not know much about it. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope everyone can gain something according to this article.

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 Order By NewID()

The NewID() function creates a unique value of type uniqueidentifier.

The effect of the above statement is to read N records randomly from Table.

Access:

The code is as follows:

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 ID is an auto-numbering field and can be done with any number, such as 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:

The code is as follows:

Select * From TABLE Order By Rand() Limit 10 view plaincopy to clipboardprint? Select * From TABLE Order By Rand() Limit 10

Select * From TABLE Order By Rand() Limit 10

postgreSQL:

The code is as follows:

select * from glxt022 order by random() limit 5 view plaincopy to clipboardprint? select * from glxt022 order by random() limit 5

After reading the above content, do you have any further understanding of the random query methods of various databases by order by newid()? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.

Tags: Code content data database method query field function name effect numeric value more knowledge article type industry statement information channel faceted Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Tech Info Microsoft Shulou Information OPPO Reno