什么sql查询可以生成100,000个随机数据的mysql行?

时间:2015-02-26 11:21:47

标签: mysql sql innodb

我有12列的表,如何通过sql查询我可以在每行的行中插入100个随机符号来生成10万行

1 个答案:

答案 0 :(得分:1)

您必须使用某个程序来完成此任务:

DELIMITER $$
CREATE PROCEDURE InsertRandomRows(IN NumRows INT)
    BEGIN
        DECLARE i INT;
        SET i = 1;
        START TRANSACTION;
        WHILE i <= NumRows DO
            INSERT INTO table_name (status, tel_mobile, tel_home, age,
                call_time, created_at, fio, address, comment) VALUES (
                ROUND(RAND() * 1000000),
                ROUND(RAND() * 1000000),
                ROUND(RAND() * 1000000),
                ROUND(RAND() * 1000000),
                ROUND(RAND() * 1000000),
                ROUND(RAND() * 1000000),
                LEFT(CONCAT(
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR ))
                ), 100),
                LEFT(CONCAT(
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR ))
                ), 100),
                LEFT(CONCAT(
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR )),
                    MD5( CAST( RAND() AS CHAR ))
                ), 100)
            );
            SET i = i + 1;
        END WHILE;
        COMMIT;
    END$$
DELIMITER ;


CALL InsertRandomRows(100000);

对于我使用的文本字段&#34; MD5&#34;函数创建一个随机的32个char长字符串,连接4个字符串(32 x 4 = 128个字符)并截断前100个字符。

对于整数字段,您可以直接使用RAND生成随机数。

希望它有所帮助!

编辑:抱歉,我遇到了错误,交换了LEFT函数参数。