MySQL去重

-- 单个字段查重
SELECT  * FROM entrance_stu_data WHERE id_card_number IN ( SELECT id_card_number FROM entrance_stu_data GROUP BY id_card_number HAVING count( id_card_number ) > 1 );

-- 单个字段:去除重复的数据,保留id最小的
DELETE FROM student_dormitory WHERE id NOT IN (SELECT dt.id FROM (SELECT min(id) AS id FROM student_dormitory GROUP BY base_student_id) dt)

-- 查询多个字段的重复数据
SELECT * FROM finance_charges a WHERE( a.term_id,a.student_id ) IN ( SELECT term_id, student_id FROM finance_charges GROUP BY term_id, student_id HAVING count( * ) > 1) order by a.student_id,a.term_id

-- 删除多个字段重复数据
DELETE FROM finance_charges WHERE id IN ( SELECT id  FROM (SELECT id FROM finance_charges a WHERE (a.term_id,a.student_id)  IN ( SELECT term_id, student_id FROM finance_charges GROUP BY term_id,student_id HAVING count( * )>1) AND id NOT IN (
SELECT min( id ) id FROM finance_charges GROUP BY term_id, student_id HAVING count(*)>1)) x )

--  查找表中多余的重复记录(多个字段),不包含rowid最小的记录
SELECT id FROM finance_charges a WHERE ( a.term_id,a.student_id ) IN ( SELECT term_id, student_id FROM finance_charges GROUP BY term_id, student_id HAVING count( * ) > 1 )  AND id NOT IN (SELECT min( id ) id FROM finance_charges GROUP BY term_id, student_id HAVING count( * ) > 1 )

-- 模糊匹配清空表
SELECT CONCAT( 'TRUNCATE table ', table_name, ';' )  FROM information_schema.TABLES WHERE table_schema = 'scheam' and table_name LIKE 't_com%';
    评论
    0 评论
avatar

取消