当前位置:首页 » 存储配置 » mysql存储过程随机数

mysql存储过程随机数

发布时间: 2023-01-24 15:54:50

① Mysql产生随机数

MYSQL 取随机数
2010年04月26日 星期一 09:48
mysql 取随机数

--对一个表取任意随机数
SELECT *
FROM TMP_XF_TEST
WHERE ID >= (SELECT FLOOR(RAND() * (SELECT MAX(ID) FROM TMP_XF_TEST)))
order by id LIMIT 1;

--有条件性的取随机数
SELECT *
FROM TMP_XF_TEST
WHERE ID >= (SELECT FLOOR(RAND() *
((SELECT MAX(ID) FROM TMP_XF_TEST WHERE GID = 9) -
(SELECT MIN(ID) FROM TMP_XF_TEST WHERE GID = 9))) +
(SELECT MIN(ID) FROM TMP_XF_TEST WHERE GID = 9))
AND GID = 9
ORDER BY ID LIMIT 1;

--gid上存在索引

或者
SELECT *
FROM TMP_XF_TEST AS t1 JOIN
(SELECT ROUND(RAND() * ((SELECT MAX(id) FROM TMP_XF_TEST WHERE GID = 9)-(SELECT MIN(id) FROM TMP_XF_TEST WHERE GID = 9))
+(SELECT MIN(id) FROM TMP_XF_TEST WHERE GID = 9)) AS id) AS t2
WHERE t1.id >= t2.id AND t1.GID = 9
ORDER BY t1.id LIMIT 1;

#########
不要用下面的杯具写法

mysql> insert into tmp_xf_test(user_nick,gid,item_id,gmt_create,gmt_modified,memo)
-> select user_nick,gid,item_id,gmt_create,gmt_modified,memo from tmp_xf_test;
Query OK, 165888 rows affected (9.65 sec)
Records: 165888 Duplicates: 0 Warnings: 0

mysql> SELECT *
-> FROM `tmp_xf_test`
-> WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `tmp_xf_test` )
-> ORDER BY id LIMIT 1;
+-----+-----------+-----+---------+---------------------+---------------------+--------------------+
| id | user_nick | gid | item_id | gmt_create | gmt_modified | memo |
+-----+-----------+-----+---------+---------------------+---------------------+--------------------+
| 467 | 玄风 | 9 | 123 | 2010-04-26 14:56:39 | 2010-04-26 14:56:39 | 玄风测试使用的数据 |
+-----+-----------+-----+---------+---------------------+---------------------+--------------------+
1 row in set (51.12 sec)

mysql> explain SELECT *
-> FROM `tmp_xf_test`
-> WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `tmp_xf_test` )
-> ORDER BY id LIMIT 1\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: tmp_xf_test
type: index
possible_keys: NULL
key: PRIMARY
key_len: 8
ref: NULL
rows: 1
Extra: Using where
*************************** 2. row ***************************
id: 2
select_type: UNCACHEABLE SUBQUERY
table: tmp_xf_test
type: index
possible_keys: NULL
key: idx_tmp_xf_test_gid
key_len: 4
ref: NULL
rows: 331954
Extra: Using index
2 rows in set (0.01 sec)

---

mysql> SELECT * FROM `tmp_xf_test` t1 join
-> (SELECT FLOOR( MAX(id) * RAND()) as id FROM `tmp_xf_test` ) as t2
-> where t1.id >=t2.id
-> ORDER BY t1.id LIMIT 1;
+-------+-----------+-----+---------+---------------------+---------------------+--------------------+-------+
| id | user_nick | gid | item_id | gmt_create | gmt_modified | memo | id |
+-------+-----------+-----+---------+---------------------+---------------------+--------------------+-------+
| 40311 | 玄风 | 9 | 123 | 2010-04-28 15:47:19 | 2010-04-28 15:47:19 | 玄风测试使用的数据 | 40311 |
+-------+-----------+-----+---------+---------------------+---------------------+--------------------+-------+
1 row in set (0.14 sec)

##############

mysql> SELECT * FROM `tmp_xf_test`
-> WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `tmp_xf_test`)))
-> ORDER BY id LIMIT 1;
+------+-----------+-----+---------+---------------------+---------------------+--------------------+
| id | user_nick | gid | item_id | gmt_create | gmt_modified | memo |
+------+-----------+-----+---------+---------------------+---------------------+--------------------+
| 1352 | 玄风 | 9 | 123 | 2010-04-28 15:47:19 | 2010-04-28 15:47:19 | 玄风测试使用的数据 |
+------+-----------+-----+---------+---------------------+---------------------+--------------------+
1 row in set (0.00 sec)

mysql> explain SELECT * FROM `tmp_xf_test`
-> WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `tmp_xf_test`)))
-> ORDER BY id LIMIT 1\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: tmp_xf_test
type: index
possible_keys: NULL
key: PRIMARY
key_len: 8
ref: NULL
rows: 1
Extra: Using where
*************************** 2. row ***************************
id: 3
select_type: SUBQUERY
table: NULL
type: NULL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: NULL
Extra: Select tables optimized away
2 rows in set, 1 warning (0.00 sec)

对应的另外一种杯具写法是:

SELECT *
FROM TMP_XF_TEST
WHERE ID >= (SELECT FLOOR(RAND() * (MAX(ID) - MIN(ID))) + MIN(ID) MID
FROM TMP_XF_TEST
WHERE GID = 9)
AND GID = 9 LIMIT 1;

② 关于mysql 的存储过程

mysql> delimiter //
这个作用是把;变成//,以后的语句遇到//就结束了,遇到;不结束,下面就可以按你的想法写了

mysql> CREATE PROCEDURE simpleproc (OUT param1 INT)
-> BEGIN
-> SELECT COUNT(*) INTO param1 FROM t;
-> END
-> //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ; 这里把双引号改回来

声明参数要在说明是 输入还是输出函数 in\out

给函数变量赋值用
set @a=10;

例子:

drop procere if exists pr_param_in;

create procere pr_param_in
(
in id int -- in 类型的 MySQL 存储过程参数
)
begin
if (id is not null) then
set id = id + 1;
end if;

select id as id_inner;
end;
set @id = 10;

call pr_param_in(@id);

select @id as id_out;
mysql> call pr_param_in(@id);

③ mysql 存储过程

字符串连接要用CONCAT('d:/',youF,'.xls')

④ MYSQL用存储过程求1到100累加

1、在单元格A1中输入数字1,向下拖动填充到100行。

⑤ mysql数据库 如果数据库中有800万条数据,我想随机抽取10000条,要怎么做抽取的更快

什么叫抽取的更快?和什么比更快?你现在是怎么做的?

数据库性能是和很多因素有关的:

  1. 想要数据库响应的快,首先要有好的服务器。

  2. 如果数据库是在远程服务器上,还要有充足和流畅的带宽网络。

  3. 合理安排表的结构,建立索引。

    针对你这个,800万条数据如果在一个表里,要有个整数型的ID作为主键,并做索引。如果数据是从不同的表里抽出来再组合起来的,表与表之间的链接键尽量用整数型并做索引。

    然后生成10000个随机数,在ID里查找这1万个数字,取出对应的数据。

  4. 处理过程放到数据库端。

  5. 针对你这个,10000个随机数的生成函数用存储过程的形式存在服务器端。

⑥ mysql为什么我不能看到别人写的存储过程

没有rando函数。只有rand和randn
1.rand()
生成(0,1)区间上均匀分布的随机变量。基本语法:
rand([M,N,P ...])
生成排列成M*N*P... 多维向量的随机数。如果只写M,则生成M*M矩阵;如果参数为[M,N]可以省略掉方括号。一些例子:
rand(5,1) %生成5个随机数排列的列向量,一般用这种格式
rand(5) %生成5行5列的随机数矩阵
rand([5,4]) %生成一个5行4列的随机数矩阵
生成的随机数大致的分布。
x=rand(100000,1);
hist(x,30);
由此可以看到生成的随机数很符合均匀分布。(视频教程会略提及hist()函数的作用)
2.randn()
生成服从标准正态分布(均值为0,方差为1)的随机数。基本语法和rand()类似。
randn([M,N,P ...])
生成排列成M*N*P... 多维向量的随机数。如果只写M,则生成M*M矩阵;如果参数为[M,N]可以省略掉方括号。一些例子:
randn(5,1) %生成5个随机数排列的列向量,一般用这种格式
randn(5) %生成5行5列的随机数矩阵
randn([5,4]) %生成一个5行4列的随机数矩阵
3、matlab中random函数——通用函数,求各分布的随机数据,其用法:
y = random('norm',A1,A2,A3,m,n)
式中:A1,A2,A3为分布的参数,m,n用来指定随机数的行和列,name的取值有相关的表格来参照。
例:产生一个3行4列均值为2、标准差为0.3的正态分布随机数:
>>y =random('norm',2,0.3,3,4)
y =

2.1613 2.2587 1.8699 2.8308
2.5502 2.0956 2.1028 1.5950
1.3223 1.6077 3.0735 2.9105

⑦ mysql用存储过程计算数据

创建:delimiter//createproceremy_add(INaint,INbint,OUTcint)beginifaisnullthenseta=0;endif;ifbisnullthensetb=0;endif;setc=a+b;end;//delimiter;查看:方法一:(直接查询,比较实用,查看当前自定义的存储过程)select`specific_name`frommysql.procwhere`db`='your_db_name'and`type`='procere'方法二:(查看数据库里所有存储过程+内容)showprocerestatus;方法三:(查看当前数据库里存储过程列表)selectspecific_namefrommysql.proc;方法四:(查看某一个存储过程的具体内容)selectbodyfrommysql.procwherespecific_name='your_proc_name';查看存储过程或函数的创建代码:showcreateprocereyour_proc_name;showcreatefunctionyour_func_name;调用:mysql>set@a=10;QueryOK,0rowsaffected(0.00sec)mysql>set@b=20;QueryOK,0rowsaffected(0.00sec)mysql>set@c=0;QueryOK,0rowsaffected(0.00sec)mysql>select@c;+------+|@c|+------+|0|+------+mysql>callmy_add(@a,@b,@c);QueryOK,0rowsaffected(0.00sec)mysql>select@a,@b,@c;+------+------+------+|@a|@b|@c|+------+------+------+|10|20|30|+------+------+------+1rowinset(0.00sec)删除dropprocereyour_proc_name;

⑧ mysql 随机产生四位数的问题

搞不懂,挺简单的一个问题,你搞的这么复杂呢?

直接这样不就可以了。

selectright(concat('0000',cast(FLOOR((RAND()*9999))aschar)),4);

基本就是无论随机数取到的是什么,前边都补4个0,然后截取右边的后四位。

⑨ mysql 存储过程

你应该在做统计吧,估计你不会的就是mysql存储过程的语法 我之前也写过 很是郁闷 我给你一段代码 是我用mysql写过的一个存储过程 你看看 主要是了解里面的语法 看懂了 你所说的需求并不难
有看不懂的地方一起讨论 :
begin

declare tikk datetime ;
declare done int default 0;
declare userid int default 0;
declare moleid int default 0;
declare couid int default 0;
declare mname varchar(255) ;
declare opsid int default 0;

declare c1 cursor for Select I_userID,I_operationID from space_operation_record where status<>0 group by I_userID,I_operationID order by createtime desc;
declare continue handler for sqlstate '02000' set done =1;
set tikk = now();
open c1;
repeat
fetch c1 into userid, opsid;
if not done then
select I_moleID from space_operation where status<>0 and ID=opsid into moleid;
if moleid <> '' then
select Nvc_identification from space_operation where status<>0 and ID=opsid into @identiftion;
if moleid > 0 then
Select Nvc_ename from space_mole where status<>0 and ID=moleid into mname;
else
set mname = 'space';
end if;

create temporary table if not exists sp_tab1(id bigint(20),Nvc_content MEDIUMTEXT,I_obyuID bigint(20),I_tID bigint(20),createtime datetime);
INSERT INTO sp_tab1 Select ID,Nvc_content,I_objectID,I_tmID,createtime from space_operation_record where status<>0 and I_operationID=opsid and I_userID=userid ;
select count(*) from sp_tab1 into couid;

set @ihod = 0;
set @listp = '';
set @listpp = '';
set @content0p = '';
set @content0 = '';
while couid > 0 do
select ID,Nvc_content,I_obyuID,createtime,I_tID into @iok,@conuiy,@objiplk,@crtimhr,@tmids from sp_tab1 where ID > @ihod order by ID asc limit 0,1;
if @iok <> '' then
if mname = 'blog' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,tikk);
elseif mname = 'team' then
if(@identiftion = 'addblog' || @identiftion = 'mdyblog') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listpp = CONCAT(@listpp,CONCAT(@objiplk,','));
set @operarry1p = substring_index(@conuiy,'|',1);
set @operarry2p = substring_index(@conuiy,'|',-1);
set @content0p = CONCAT(@content0p,CONCAT(@operarry2p,SPACE(1)));
set @objlistp = substring(@listpp,1,length(@listpp)-1);
end if;
elseif mname = 'space' then
if(@identiftion = 'headphoto' || @identiftion = 'status') then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,I_tmID,createtime) VALUES (@iok,userid,@conuiy,@crtimhr,@tmids,tikk);
else
set @listppr = CONCAT(@listppr,CONCAT(@objiplk,','));
set @operarry1pr = substring_index(@conuiy,'|',1);
set @operarry2pr = substring_index(@conuiy,'|',-1);
set @content0pr = CONCAT(@content0pr,CONCAT(@operarry2pr,SPACE(1)));
set @objlistpr = substring(@listppr,1,length(@listppr)-1);
end if;
else
set @listp = CONCAT(@listp,CONCAT(@objiplk,','));
set @operarry1 = substring_index(@conuiy,'|',1);
set @operarry2 = substring_index(@conuiy,'|',-1);
set @content0 = CONCAT(@content0,CONCAT(@operarry2,SPACE(1)));
set @objlist = substring(@listp,1,length(@listp)-1);
end if;
set @ihod = @iok;
end if;
set couid = couid -1;
end while;

if @content0 <> '' then
set @contentp = CONCAT(@operarry1,concat('|',@content0));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlist);
end if;
end if;
if @content0p <> '' then
if @identiftion = 'addphoto' then
set @contentp = CONCAT(@operarry1p,CONCAT('|',@content0p));
else
set @contentp = CONCAT(@operarry1p,CONCAT(@content0p,'|'));
end if;

Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
if @content0pr <> '' then
set @contentp = CONCAT(@operarry1p,concat('|',@content0pr));
Select createtime,ID into @uitimej,@IDjok from space_operation_record where status<>0 and I_operationID=opsid order by createtime desc limit 0,1;
if @uitimej <> '' then
INSERT INTO space_operation_stat(I_operationID,I_userID,Nvc_content,D_stattime,createtime,Nvc_objlist,I_tmID) VALUES(@iok,userid,@contentp,@crtimhr,tikk,@objlistp,@tmids);
end if;
end if;
delete from sp_tab1;
end if;
end if;

until done end repeat;
close c1;
drop temporary table if exists sp_tab1 ;

UPDATE space_operation_play SET status=0;
UPDATE space_operation_display SET status=0;
Select createtime into @ptimes from space_operation_stat where status<>0 order by createtime desc limit 0,1;
if @ptimes <>'' then
create temporary table if not exists sp_tab2(id bigint(20),Nvc_content MEDIUMTEXT,I_userID bigint(20),I_lyuID bigint(20),D_stattime datetime);
INSERT INTO sp_tab2 Select ID,Nvc_content,I_userID,I_tmID,D_stattime from space_operation_stat where status<>0 and createtime=@ptimes order by D_stattime desc limit 0,30;
select count(*) from sp_tab2 into @cou1id;
set @uoj = 0;
while @cou1id > 0 do
select ID,Nvc_content,I_userID,D_stattime,I_lyuID into @io1k,@conui1y,@objipl1k,@crtimh1r,@unlpa from sp_tab2 where ID > @uoj order by ID asc limit 0,1;
if @io1k <> '' then
INSERT INTO space_operation_play(I_statID,Nvc_content,D_stattime,I_userID,Createtime,I_tmID) VALUES (@io1k,@conui1y,@crtimh1r,@objipl1k,now(),@unlpa);
set @uoj = @io1k;
end if;
set @cou1id = @cou1id -1;
end while;
drop temporary table if exists sp_tab2 ;
end if;

end

热点内容
phpcgi与phpfpm 发布:2025-07-19 02:05:19 浏览:522
捷达方向机安全登录密码是多少 发布:2025-07-19 00:57:37 浏览:689
夜魔迅雷下载ftp 发布:2025-07-19 00:39:29 浏览:94
增值税票安全接入服务器地址 发布:2025-07-19 00:20:45 浏览:481
solidworkspcb服务器地址 发布:2025-07-18 22:50:35 浏览:818
怎么在堆叠交换机里配置vlan 发布:2025-07-18 22:42:35 浏览:627
java调用别人的接口 发布:2025-07-18 22:37:35 浏览:437
服务器四个节点如何联网 发布:2025-07-18 22:36:02 浏览:275
华强北什么地方休安卓手机 发布:2025-07-18 22:24:56 浏览:738
数据库的根本目标 发布:2025-07-18 21:37:50 浏览:941