mysql clearly leaving only a plurality of columns wherein a duplicate data

-- 创建临时表存储查询出的数据
drop temporary table if exists childtable;
create temporary table childtable (select * from tb_child a where 
(a.patriarch_id,a.student_id) in (select patriarch_id,student_id from tb_child group by patriarch_id,student_id 
having count(*) > 1)
and child_id not in (select min(child_id) from tb_child group 
by patriarch_id,student_id having count(*)>1));

select *ChildTable the From 
drop the Temporary ChildTable the Table;
 - according to the conditions deleting data in the temporary table 
the Delete from tb_child   the WHERE   child_id in ( the SELECT childtable.child_id from ChildTable)

 - check out the binding relationship table id parents and student id duplicate data. But does not include binding relationship that record the smallest id
 the SELECT * from tb_child A the WHERE  
(a.patriarch_id, a.student_id) in ( the SELECT patriarch_id, student_id from tb_child Group by patriarch_id, student_id 
the HAVING COUNT ( *)> 1 ) 
and child_id not in (SELECT min (child_id) from tb_child Group 
by patriarch_id, student_id HAVING COUNT ( *)> . 1 )

 - Query having patriarch_id, student_id duplicated data (id minimum negative first data) with line numbers
 SELECT (@rownum: = + @rownum . 1 ) the AS rownum, A *. from tb_child A, (the SELECT @rownum: = 0 ) the AS RN   WHERE  
(a.patriarch_id, a.student_id) in ( SELECT patriarch_id, the student_id from tb_child by patriarch_id Group, the student_id 
HAVING COUNT ( *)> 1 ) 
and child_id not in ( the SELECT min(child_id) from tb_child group 
by patriarch_id,student_id having count(*)>1)

--查询出具有patriarch_id ,student_id 重复的数据 带行号
select (@rownum := @rownum + 1) AS rownum ,ci.* from tb_child ci, (SELECT 
    @rownum := 0) AS rn  where patriarch_id in (select patriarch_id from tb_child group by patriarch_id,student_id having count(*) > 1) 

 

Guess you like

Origin www.cnblogs.com/chongyao/p/12667942.html