【mysql】跨数据库同步数据

平时不怎么写存储过程,基本要用到的时候就到网上搜索,刚刚完成一个存储过程

大致作用是两个数据库,都有结构差不多的表,现在需要将一个库里该表同步到另一个库中,建了一个存储过程,并准备后面让这个存储过程自动2小时执行(这个用事件)


begin 
	declare  b int;
	declare id1, user_name1, name1, password1, salt1 VARCHAR(100);
	DECLARE userType int;
	declare count, grade1 int;
	DECLARE count1 int;
	declare  cur_1  cursor for
	select id, username, `name`, `password`, salt, user_type from sys_user where `status`=1 and (user_type=4 or user_type=5);
	SET b = 0;
	select count(1)  INTO count from sys_user where `status`=1;
	#select count;
	OPEN cur_1;
    loop_label: loop
			FETCH cur_1 INTO id1, user_name1, name1, password1, salt1, userType;
				
				select count(1) into count1 from test1.sys_user where username=user_name1;
				#select count1;
				if count1 = 0 THEN
					if userType=4 THEN
						insert into test1.sys_user(id, username, `name`, `password`, salt, create_date, user_type, relative_school_id, `status`, avatar) 
							VALUES (id1,user_name1, name1, password1, salt1, now(), 3, '0140bc57-de2b-44a8-8039-e475d180db8a', 1, '/filesystem/headImg/default.jpg');
					end if;
					if userType=5 THEN
						select tci.grade into grade1 from t_student_info tsi left join t_class_info tci on tsi.class_id=tci.id where tsi.user_id=id1 and tsi.`status`=1;
						insert into test1.sys_user(id, username, `name`, `password`, salt, create_date, user_type, relative_school_id, `status`, avatar) 
							VALUES (id1,user_name1, name1, password1, salt1, now(), 4, '0140bc57-de2b-44a8-8039-e475d180db8a', 1, '/filesystem/headImg/default.jpg');
						insert into test1.klss_student(id, user_id, grade_num , pass_count, challenge_count, homework_count, total_integral, `level`, phone, badge_num, sex) 
							VALUES(UUID(), id1, grade1, 0, 0, 0, 0, 1, "", 0, 0);
					end if;
				end if;
				#select user_name1;
				
				set b=b+1;
			if b = count    then
          leave loop_label;
      end if;

    end loop;
    CLOSE cur_1; 
end


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值