mysql 存储过程 字段去重

BEGIN
  DECLARE bl_id int;
  DECLARE from_ids varchar(100);   
  DECLARE bl_from_id varchar(100);#中间变量
  DECLARE i int;

  DECLARE Done INT DEFAULT 0;
  DECLARE IMP_TABLE_CUR CURSOR FOR 
             (select id,from_id from weixin_fromid ORDER BY from_id); #from_id要去重的字段
  DECLARE IMP_TABLE_VALID_CUR CURSOR FOR 
           select id,from_id from weixin_fromid ORDER BY from_id; 
  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET Done = 1;

  set bl_from_id='a';#选一个from_id达不到的值
  set i=0;
  open IMP_TABLE_CUR;
          
    FETCH next from IMP_TABLE_CUR INTO bl_id,from_ids;   -- 获取游标里的值
    repeat 
       IF NOT Done THEN
          if bl_from_id=from_ids THEN
             DELETE from weixin_fromid where id=bl_id;
             set i=i+1;
          ELSE
             set  bl_from_id=from_ids;
          end if;
 
       end if;
    FETCH next from IMP_TABLE_CUR INTO bl_id,from_ids;
  until done end repeat;

select i;
END

如果你觉得这篇内容对你挺有启发请点赞+关注

posted @ 2022-07-16 13:52  深山藏古客  阅读(50)  评论(0编辑  收藏  举报