einfügen so Ich reihe mit Zufallszahlen von 1 bis 5 in einigen Spalten.
Schema:
create table friday1
( id int auto_increment primary key,
value1 int not null,
value2 int not null
);
Insert 3 Reihen:
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
Stored Proc Helfer:
drop procedure if exists insertMany;
DELIMITER $$
create procedure insertMany
( howMany int
)
BEGIN
DECLARE soFar int default 0;
set howMany=least(howMany,500); -- max out at 500 regardless of IN parameter (RAND is slow)
WHILE soFar<howMany DO
insert friday1(value1,value2) select floor(rand()*5)+1,floor(rand()*5)+1;
set soFar=soFar+1;
END WHILE;
select soFar; -- # of times in loop
END$$
DELIMITER ;
Test:
call insertMany(200);
Ergebnisse:
select count(*) as theCount,
least(min(value1),min(value2)) as theMin,
greatest(max(value1),max(value2)) as theMax
from friday1;
+----------+--------+--------+
| theCount | theMin | theMax |
+----------+--------+--------+
| 203 | 1 | 5 |
+----------+--------+--------+
[Dieser Link] (http://www.techonthenet.com/mysql/functions/rand.php) zeigt Ihnen, wie 'RAND() verwenden' eine Zufallszahl innerhalb eines erzeugen Angebot. Sie können dies einfach in Ihre INSERT-Anweisung einfügen. –
Ich habe versucht, so in den Werten habe ich Werte ('511222', RAND() * (5-1) +1, '323632') Weil ich Nummern zwischen 1 und 5. mit MYSQL Arbeitsbank nicht sicher, ob das macht ein Unterschied – Speedychuck
@Speedychuck hat das nicht funktioniert? – apokryfos