数据库生成随机数
数据库生成随机数
Oracle随机数:
SELECT DBMS_RANDOM.RANDOM FROM DUAL; 产生一个任意大小的随机数
SELECT dbms_random.value(10,20) FROM dual; 产生一个10~20之间的随机数
SELECT ABS(MOD(DBMS_RANDOM.RANDOM,100)) FROM DUAL; 产生一个100以内的随机数
SELECT TRUNC(100+900*dbms_random.value) FROM dual; 产生一个100~1000之间的随机数
SELECT trunc(dbms_random.value(100,999)) FROM dual; 在100—999中随机取一个整数
亲测:
以下两个都可以,感觉第一个用起来顺手
SELECT TRUNC(dbms_random.value(100,900)) FROM dual;
SELECT TRUNC(100+900*dbms_random.value) FROM dual;
Mysql随机数:
1) 产生0到10000间的随机数
SELECT RAND() * 10000;
对应产生相应的整数
SELECT FLOOR(RAND() * 10000)
SELECT FLOOR(RAND() * 10000)
2) 使用md5()产生32位随机字符串
SELECT MD5(RAND() * 10000)
3) 产生500-1000间的整数
SELECT FLOOR( 500 + RAND() * (1000 - 500))
SELECT FLOOR( 500 + RAND() * (1000 - 500))
SELECT NOW()
select UNIX_TIMESTAMP();
select UNIX_TIMESTAMP();
select FROM_UNIXTIME(UNIX_TIMESTAMP());
SELECT FROM_UNIXTIME(pay_time) FROM ecs_order_info
WHERE pay_name=UNIX_TIMESTAMP()
WHERE pay_name=UNIX_TIMESTAMP()
场景测测试:假设某表如下结构
CREATE TABLE fact (
dim1 int,
dim2 int,
name varchar(20),
hash varchar(32),
measure1 double
);
则可以自动产生适合其结构的大量随机记录
create table names(id int auto_increment primary key, name varchar(20));
insert into names (name) values ('Justin','Jerry','James','Josh','Julien');
select (select name from names where id = 1 + rand() * 4);
这里有一个表专门产生随机的字符串;
INSERT INTO fact
SELECT FLOOR(1+ rand()*9999),
FLOOR(1 + rand()*499),
(select name from names where id = 1 + rand() * 4),
MD5(1+rand()*9999),
rand()
FROM fact;
CREATE TABLE fact (
dim1 int,
dim2 int,
name varchar(20),
hash varchar(32),
measure1 double
);
则可以自动产生适合其结构的大量随机记录
create table names(id int auto_increment primary key, name varchar(20));
insert into names (name) values ('Justin','Jerry','James','Josh','Julien');
select (select name from names where id = 1 + rand() * 4);
这里有一个表专门产生随机的字符串;
INSERT INTO fact
SELECT FLOOR(1+ rand()*9999),
FLOOR(1 + rand()*499),
(select name from names where id = 1 + rand() * 4),
MD5(1+rand()*9999),
rand()
FROM fact;
出处:https://www.cnblogs.com/dersome/