SQL 隨機查詢 包括(sqlserver,mysql,access等)

來源:互聯網
上載者:User

SQL Server:
Sql代碼
複製代碼 代碼如下:
Select TOP N * From TABLE Order By NewID()

NewID()函數將建立一個 uniqueidentifier 類型的唯一值。上面的語句實現效果是從Table中隨機讀取N條記錄。
Access:
Sql代碼
複製代碼 代碼如下:
Select TOP N * From TABLE Order By Rnd(ID)

Rnd(ID) 其中的ID是自動編號欄位,可以利用其他任何數值來完成,比如用姓名欄位(UserName)
Sql代碼
複製代碼 代碼如下:
Select TOP N * From TABLE Order BY Rnd(Len(UserName))

MySql:
Sql代碼
複製代碼 代碼如下:
Select * From TABLE Order By Rand() Limit 10


postgreSQL
Sql代碼
複製代碼 代碼如下:
select * from glxt022 order by random() limit 5

相關文章

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.