order by newid() 各种数据库随机查询的方法

编程学习 2021-07-05 13:42www.dzhlxh.cn编程入门
order by newid() 各种数据库随机查询的方法,需要的朋友可以参考一下

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()


NewID()函数将创建一个 uniqueidentifier 类型的唯一值。上面的语句实现效果是从Table中随机读取N条记录。

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) 其中的ID是自动编号字段,可以利用其他任何数值来完成,比如用姓名字段(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 10  
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

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板