表結構:函數
經過分組找出篩選出count大於1的數據spa
select r_content, r_userid, count(*) from reviews GROUP BY r_content, r_userid having count(*) > 1
group by能夠利用聚合函數(count、 sum、 avg)進行分組設計
having 對分組的數據進行下一步篩選code
經過inner join找出相同數據blog
select * from reviews a INNER JOIN (select r_content, r_userid, count(*) from reviews GROUP BY r_content, r_userid having count(*) > 1) b on a.r_content = b.r_content and a.r_userid = b.r_userid;
加行號md5
select a.*, if(@tmp = CONCAT(r_content, r_userid), @rownum := @rownum + 1, @rownum := 1) as rownum, @tmp := CONCAT(r_content, r_userid) from ( select a.* from reviews a INNER JOIN (select r_content, r_userid, count(*) from reviews GROUP BY r_content, r_userid having count(*) > 1) b on a.r_content = b.r_content and a.r_userid = b.r_userid) a, (select @rownum := 0, @tmp := '') b
取出行號大於的刪除之it
delete from reviews where r_id in( select r_id from ( select a.*, if(@tmp = CONCAT(r_content, r_userid), @rownum := @rownum + 1, @rownum := 1) as rownum, @tmp := CONCAT(r_content, r_userid) from ( select a.* from reviews a INNER JOIN (select r_content, r_userid, count(*) from reviews GROUP BY r_content, r_userid having count(*) > 1) b on a.r_content = b.r_content and a.r_userid = b.r_userid) a, (select @rownum := 0, @tmp := '') b) a where rownum>1);
經過group_concat來組合id,並刪除class
select group_concat(ids separator '|') as ids from( select group_concat(r_id) as ids ,r_content,r_userid,count(*) from reviews group by r_content,r_userid having count(*)>1) a
表設計:date
需求:若是有相同內容則不插入,記錄插入相同數據的條數select
insert into news (news_title, news_abstract,news_code) values ('這是一條新聞','新聞內容是XXX',md5(concat('這是一條新聞','新聞內容是XXX'))) on duplicate key update dumpnum=dumpnum+1;