代码之家  ›  专栏  ›  技术社区  ›  Adeptus Mechanicus

如何通过一个sql请求向mysql表中插入一定数量的行

  •  0
  • Adeptus Mechanicus  · 技术社区  · 6 年前

    我遇到了一种情况,当我需要用一个sql请求在mysql表中插入一些行时。使用php很简单,但我只需要使用sql请求即可。mysql中有类似“for”的东西吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   JohnR    6 年前

    是的,你可以这样做,也许对你有帮助。

    drop procedure if exists load_foo_test_data;
    
    delimiter #
    create procedure load_foo_test_data()
    begin
    
    declare v_max int unsigned default 1000;
    declare v_counter int unsigned default 0;
    
        truncate table foo;
        start transaction;
        while v_counter < v_max do
            insert into foo (val) values ( floor(0 + (rand() * 65535)) );
            set v_counter=v_counter+1;
        end while;
        commit;
    end #
    delimiter ;
    
    call load_foo_test_data();
    
    select * from foo order by id;