Home > Database > Mysql Tutorial > body text

Atitit.并发测试解决方案(2)-----获取随机数据库记录随机抽取数

WBOY
Release: 2016-06-07 16:01:10
Original
993 people have browsed it

Atitit.并发测试解决方案(2) -----获取随机数据库记录 随机抽取数据 随机排序 1. 应用场景 1 2. 随机抽取数据原理 1 3. 常用的实现方法:::数据库随机函数 1 4. Mssql 的实现 NEWID() 跟rand() 1 5. newid()与rand()的区别 2 6. NEWID() 2 7. 参考 2 1. 应用

Atitit.并发测试解决方案(2) -----获取随机数据库记录 随机抽取数据 随机排序

1. 应用场景 1

2. 随机抽取数据原理 1

3. 常用的实现方法:::数据库随机函数 1

4. Mssql 的实现 NEWID() 跟rand() 1

5. newid()与rand()的区别 2

6. NEWID() 2

7. 参考 2

1. 应用场景

并发测试

2. 随机抽取数据原理

原理是 循环所有的ID/记录,附加随机函数字段,然后排序as 这个字段..

3. 常用的实现方法:::数据库随机函数

作者::老哇的爪子Attilax艾龙,EMAIL:1466519819@qq.com

转载请注明来源:http://blog.csdn.net/attilax

4. Mssql 的实现 NEWID() 跟rand()

SELECT TOP 10 *

FROM wechatuhstry

ORDER BY NEWID()

或者

select top 10 *,newid() as Random from ywle where ywlename='001' ordey by Random 

随机函数:rand()
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会 取随机整数。那就看下面的两种随机取整数的方法:

应该使用newid 不是rand()..newid每row不一样....rand每次数不一样.

它返回一个GUID

5. newid()与rand()的区别

select top 3 id from table order by newid() 这一条每运行一次,都产生新的一个随机的三行结果

而 select top 3 id from table order by Rand() 运行后每一次的三条记录是一样的

Rand()函数在给定命令里所有行都返回相同的值

你可能不需要得到这样的结果,则可以给rand()函数提供一个种子值。在下面的代码中,我们在Rand()函数中给定每一行的productid作为种子值。

6. NEWID()

4650D5AA-E8FE-4721-83E7-EA12C4F7A473

它返回一个GUID

newdi()创建 uniqueidentifier 类型的唯一值,类似这样的样式 6F9619FF-8B86-D011-B42D-00C04FC964FF
由于select每当扫描得到一条记录时候均调用该函数,这样产生每条结果记录的各不相同值,如果按照此值排序,就类似于产生了随机记录的效果

7. 参考

数据库查询排序使用随机排序结果示例(Oracle_MySQL_MS SQL Server)_oracle_脚本之家.htm

SQL NEWID()随机函数 - 兰松的日志 - 网易博客.htm

sql server的随机函数newID()和RAND() - 无意 - 博客园.htm

SQL随机函数NEWID()和RAND() - xiao_mege的日志 - 网易博客.htm

newid()与rand()的区别小谈 - 远方 - 博客园.htm

我要啦免费统计
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!