如何插入随机数MySql [英] How to insert random numbers MySql

查看:82
本文介绍了如何插入随机数MySql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MySql的新手,我需要在表格中插入10000个随机2位数字.有简单的方法吗?

解决方案

尝试一下:

DELIMITER $$
CREATE PROCEDURE random_fill( IN cnt INT )
BEGIN

    fold: LOOP

         IF cnt < 1 THEN
             LEAVE fold;
         END IF;

        INSERT INTO foo ( bar ) VALUES ( 9 + CEIL( RAND() * 90 ) );

        SET cnt = cnt - 1;
    END LOOP fold;

END$$   
DELIMIMTER ;

要使用此功能:

CALL random_fill(10000);

您需要更改的是此行:

INSERT INTO foo ( bar ) VALUES ( CEIL( RAND() * higher ) );

用数据库中存在的内容替换foobar. 可以创建一个过程,其中还将表和行的名称作为参数提供,但这需要快速进行CONCAT查询. /p>

Im new to MySql, and I need to insert into table 10000 random 2-digit numbers. Is there Easy way to to this?

解决方案

Try this:

DELIMITER $$
CREATE PROCEDURE random_fill( IN cnt INT )
BEGIN

    fold: LOOP

         IF cnt < 1 THEN
             LEAVE fold;
         END IF;

        INSERT INTO foo ( bar ) VALUES ( 9 + CEIL( RAND() * 90 ) );

        SET cnt = cnt - 1;
    END LOOP fold;

END$$   
DELIMIMTER ;

To use this:

CALL random_fill(10000);

What you have to change is this line:

INSERT INTO foo ( bar ) VALUES ( CEIL( RAND() * higher ) );

Replace the foo and bar with something that exists in your database. It is possible to create a procedure, where the name of table and row are also supplied as parameters, but that would require to CONCAT the query on fly .. looks hack'ish and ugly.

这篇关于如何插入随机数MySql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆