當前位置:首頁 » 存儲配置 » 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

熱點內容
華強北什麼地方休安卓手機 發布:2025-07-18 22:24:56 瀏覽:735
資料庫的根本目標 發布:2025-07-18 21:37:50 瀏覽:938
壓縮機的流速 發布:2025-07-18 21:37:40 瀏覽:407
三星怎麼取消手機密碼 發布:2025-07-18 21:33:50 瀏覽:630
安卓手機耳機如何彈窗顯示電量 發布:2025-07-18 21:20:53 瀏覽:59
雲伺服器搭建需要什麼工具 發布:2025-07-18 20:51:08 瀏覽:322
如何提高手機緩存速度 發布:2025-07-18 20:24:48 瀏覽:237
vba讀取資料庫數據 發布:2025-07-18 20:24:48 瀏覽:609
shell解壓zip 發布:2025-07-18 20:20:36 瀏覽:861
安卓泰拉瑞亞去哪裡買 發布:2025-07-18 20:01:05 瀏覽:694