mysql通过子查询的方式插入数据

drop procedure if exists test;          
create procedure test()                 
begin
    declare i int;      #申明变量
    declare count int;                      #申明变量
    set i = 0;                          #变量赋值
        set count=(select count(*) from app_device);  #变量赋值
    while i <count do                     #结束循环的条件: 当i大于count时跳出while循环
        INSERT INTO `app_device_contact` (`serial_number`, `tel`) VALUES (
                (select serial_number from app_device order by device_id asc limit i,1), '3');                                                        
        set i = i + 1;                  #循环一次,i加一
    end while;           
        select * from app_device_contact;                   #查看test表数据
end
call test();

posted @ 2019-12-16 15:04  IAmSao瑞  阅读(1587)  评论(0)    收藏  举报