mysql 儲存過程刪除重複資料

2021-09-26 04:05:49 字數 3021 閱讀 2274

create table `blog_article` (

`uid` varchar(64) not null,

`rangid` bigint(20) not null default '0',

`lon` varchar(32) not null,

`lat` varchar(32) not null,

`name` varchar(64) null default null

)collate='utf8_general_ci'

engine=innodb

; insert into blog_article values ('123', '111', '100','11','zzz');

insert into blog_article values ('123', '112', '100','11','zzz');

insert into blog_article values ('121', '222', '200','21','qq');

insert into blog_article values ('121', '221', '200','21','qq');

insert into blog_article values ('120', '333', '300','33','qa');

insert into blog_article values ('120', '332', '300','33','qa');

insert into blog_article values ('119', '444', '44','400','mm');

insert into blog_article values ('119', '441', '44','400','mm');

insert into blog_article values ('118', '555', '55','500','nnn');

insert into blog_article values ('118', '554', '55','500','nnn');

insert into blog_article values ('117', '666', '66','600','kkk');

insert into blog_article values ('117', '606', '66','600','kkk');

insert into blog_article values ('116', '777', '77','700','ddd');

insert into blog_article values ('116', '787', '77','700','ddd');

insert into blog_article values ('115', '888', '800','800','ff');

insert into blog_article values ('115', '888', '800','88','ff');

insert into blog_article values ('114', '999', '900','88','pp');

insert into blog_article values ('114', '999', '900','99','pp');

drop procedure if exists proc_cursor;

delimiter $$

create procedure proc_cursor()

begin

declare done int default false; -- 遍歷資料結束標誌定義

declare struid varchar(64);

declare rangidint bigint(20);

declare strlon varchar(32);

declare strlat varchar(32);

declare rs cursor for select uid, rangid,lon,lat from testdb.blog_article where uid in (select uid from (select uid,count(1) as num from blog_article group by uid) t where t.num>1); -- 定義游標

declare continue handler for sqlstate '02000' set done = true; -- 游標結束/異常處理

open rs; -- 結束游標

fetch next from rs into struid, rangidint,strlon,strlat; -- 遍歷游標取值

repeat

if not done then

-- fetch next from rs into strname, straddress; -- 遍歷游標取值放在此處則後面不需要再放

start transaction; -- 開始事務

if (convert(strlon, signed) + convert(strlat, signed)) != rangidint then

insert into testdb.blog_article_copy(uid, rangid,lon,lat) values( struid, rangidint, strlon, strlat);

delete from testdb.blog_article where uid = struid and rangid = rangidint;

end if;

commit; -- 提交事務

end if;

fetch next from rs into struid, rangidint,strlon,strlat; -- 迴圈遍歷處理

until done end repeat; -- 結束遍歷

close rs; -- 關閉游標

end$$

delimiter ;

call proc_cursor();

注意:定義變數名稱時,變數名要與原表中的欄位名稱有區別,因為mysql不區分大小寫;

mysql刪除重複資料

最近遇到刪除重複資料的問題,先分享一下解決辦法,如有不完善之處還望包涵!舉例如下 mysql select from table03 id name degree 1 fly 90 2 fly 90 3 fly 90 4 fly 80 5 wang 90 6 wang 90 7 wang 90 8 ...

MySQL 刪除重複資料

建表,插入資料,當然如果你不想測試一下,也可以直接用後面的刪除語句 create table if not exists tb01 id int unsigned primary key auto increment,name varchar 10 not null insert into tb01...

mysql刪除重複資料

id 姓名 課程名稱 分數 1 張三 數學 69 2 李四 數學 89 3 張三 數學 69 刪除除了自動編號不同,其他都相同的學生冗餘資訊 完整的sql語句如下 delete from tablename where id not in select bid from select min id ...