如何在My SQL中的列中插入500,000个随机值?

时间:2015-11-18 04:18:02

标签: mysql sql-server

如果我有一个名为page_info的表和三列id INT, visitor_id INT, duration DEC(4,1),我如何为每列添加500,000个随机值?例如,如果我想要总共500,000行,我希望我的id在1-500之间,我希望我的visitor_id在1-10,000之间,我希望我的持续时间可以在任何地方在000.1-240.0之间。

1 个答案:

答案 0 :(得分:0)

创建存储过程,例如:

CREATE PROCEDURE `new_procedure`()
BEGIN
    DECLARE i INT DEFAULT 0;
    DECLARE vId INT DEFAULT 0;
    DECLARE vVis INT DEFAULT 0;
    DECLARE vDur DECIMAL(4,1) DEFAULT 0.0;

    WHILE i < 500001 DO

        while vId = 0 do
            set vId = FLOOR(RAND() * 500);
        end while;

        while vVis = 0 do
            set vVis = FLOOR(RAND() * 10000);
        end while;

        while vDur = 0.0 do
            set vDur = RAND() * 240;
        end while;

        INSERT page_info (id, visitor_id, duration) VALUES( vId, vVis, vDur );
        SET i = i + 1;

        IF i < 500001 THEN
            set vId = 0;
            set vVis = 0;
            set vDur = 0.0;
        END IF;

    END WHILE;
END