How to create arbitrary characters and paste in MySQL?

Duplicate:
Insert random characters into MYSQL database

How can I generate 100 records with 5 random characters and paste into the database with the query.

I want to insert into this table:

codes id (auto-increment) codes 
+4
source share
4 answers

Try this option -

 SELECT CONCAT( CHAR( FLOOR(65 + (RAND() * 25))), CHAR( FLOOR(65 + (RAND() * 25))), CHAR( FLOOR(65 + (RAND() * 25))), CHAR( FLOOR(65 + (RAND() * 25))), CHAR( FLOOR(65 + (RAND() * 25))) ) random_string; 

This query generates ASCII codes from "A" to "Z" and generates a random string from them. I canโ€™t say that this method is elegant, but it works; -)

+13
source
 INSERT INTO codes_tbl (codes) VALUES (SUBSTRING(MD5(RAND()) FROM 1 FOR 5)); 

That should take care of this.

+4
source

In MySql U you can do it like this

 insert into table ( SUBSTRING(MD5(RAND()) FROM 1 FOR 10) , field2 , field3) , ( SUBSTRING(MD5(RAND()) FROM 1 FOR 10) , field2 , field3) , ......... 

If you want to do it with php. U Can Check This Link

Further you can check the following questions that are already asked in Stackoverflow

1. Random Number - MySql

2. Mysql inserts random unique 8 characters

0
source

Could you try ?:

 CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertOneHundredRandomCodes` () BEGIN DECLARE ctr INT DEFAULT 0; hundred_loop:LOOP SET ctr = ctr + 1; -- increment -- insert command here INSERT INTO codes (codes) SELECT SUBSTRING(MD5(RAND()) FROM 1 FOR 5) AS codes; IF ctr = 100 THEN -- check if we should stop LEAVE hundred_loop; END IF; END LOOP hundred_loop; END// 

Create this procedure, then run the following command:

 CALL InsertOneHundredRandomCodes(); 

It should insert codes 100 random values โ€‹โ€‹for the codes value.

0
source

Source: https://habr.com/ru/post/1382211/


All Articles