您的位置:首页 > 娱乐 > 明星 > 东莞网站建站公司_网页个人主页设计_推广竞价_阿里云域名购买

东莞网站建站公司_网页个人主页设计_推广竞价_阿里云域名购买

2024/10/5 9:21:32 来源:https://blog.csdn.net/hunheidaode/article/details/142660413  浏览:    关键词:东莞网站建站公司_网页个人主页设计_推广竞价_阿里云域名购买
东莞网站建站公司_网页个人主页设计_推广竞价_阿里云域名购买

1.需求:

users表中的数据 如果在users_copy1表中存在(2各表id相等),则根据users表的数据更新users_copy1表的数据,这两个表id相等。

例子:

users表数据:

users_copy1表数据:

当执行完:CALL testproduce();后:

users_copy1后的表数据:

例子2:

users表数据:

users_copy1表数据:

当执行完:CALL testproduce();后:

users_copy1后的表数据:

2.表结构:

CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`age` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;INSERT INTO `test`.`users_copy1` (`id`, `name`, `age`) VALUES (1, '张三', 10);
INSERT INTO `test`.`users_copy1` (`id`, `name`, `age`) VALUES (2, '李四', 20);
INSERT INTO `test`.`users_copy1` (`id`, `name`, `age`) VALUES (3, '王五', 30);CREATE TABLE `users_copy1` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(255) DEFAULT NULL,`age` int(11) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

3.存储过程:

drop PROCEDURE if exists testproduce;CREATE DEFINER=`root`@`%` PROCEDURE `testproduce`()
BEGIN#Routine body goes here...DECLARE s INT DEFAULT 0;DECLARE num INT DEFAULT 0;DECLARE ids INT DEFAULT 0;DECLARE names varchar(500) DEFAULT '';DECLARE ages INT DEFAULT 0;DECLARE list CURSOR FOR select id ids,name names,age ages from users;DECLARE CONTINUE HANDLER FOR SQLSTATE '02000'  set s=1;OPEN list;FETCH list into ids,names,ages;WHILE s <> 1 DOset num=(select count(1) counts from users_copy1 where id=ids);IF num =0 THEN insert into users_copy1 select id,name,age from users where id=ids;FETCH list into ids,names,ages;ELSEUPDATE users_copy1 set name=names,age=ages where id=ids; FETCH list into ids,names,ages;END IF;END WHILE;CLOSE list;END;
CALL testproduce();

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com