获取0 - 1之间的随机小数
select random();
获取一个1 - 10000之间的随机整数,ceil函数:得到不小于参数的最小的整数,floor:得到不大于参数的最大整数,trunc:截断
SELECT ceil(random()*(10000-1)+1) as num;
SELECT floor(random()*(10000-1)+1) as num;
SELECT trunc(random()*(10000-1)+1) as num;
获取100个1-10000之间的随机整数,可能会有重复数据
SELECT ceil(random()*(10000-1)+1) as num from generate_series(1,100);
获取100个1-10000之间的随机整数,没有重复数据
select num from generate_series(1,1000000) as t(num) order by random() limit 10;
再介绍一个生成测试数据的方法,生成2000w数据,第一个整数,第二个8位文本
select
id,substring(md5(
id::varchar),
2,
8)
from generate_series (
1,
2000000)
as t(
id)
select
id,substring(md5(random()::varchar),
2,
8)
from generate_series (
1,
2000000)
as t(
id)