mysql dynamically generates test data

  • 2020-05-07 20:35:39
  • OfStack

1. Problem
To generate two types of data:
A class: two digit 01 02 03... 09 10 11... 19, 20, 21... 98, 99,
Another class B class: 3 bits of 100 101 102... 110, 111, 112... 998, 999,
2. Solution
1, build table
 
CREATE TABLE `test`.`ta` ( 
`a` varchar(45) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

2. Create stored procedures
 
DELIMITER $$ 
DROP PROCEDURE IF EXISTS `test`.`proc_tp` $$ 
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_tp`(in prex int,in max int) 
begin 
declare i INT DEFAULT 0; 
declare s varchar(500); 
WHILE (i<10 and prex<max) DO 
select concat(prex,i) into s; 
insert into ta (a) values (s); 
set i=i+1; 
if(i=10 and prex<max) then 
set prex=prex+1; 
set i=0; 
end if; 
END WHILE ; 
end $$ 
DELIMITER ; 

3, respectively, calls the execution stored procedure
CALL proc_tp(0,10) creates A class data
CALL proc_tp(10,100) creates B class data
4. Query results
SELECT * FROM ta t order by cast(a as signed) asc;

Related articles: