https://blog.csdn.net/weixin_30410999/article/details/97993287
和这位博主类似
有一张表student 数据如下
id name subject teacher
1 张三 英语 z
2 张三 英语 z
3 李四 数学 l
4 王五 英语 z
5 王五 数学 l
6 王五 英语 z
根据 name,subject ,teacher 三个字段进行重复数据去重 相同数据 保留一条(保留哪条不做限制) ,其余删除 (可最多加一个时间字段)
例如 此表保留后如下:
id name subject teacher
1 张三 英语 z
3 李四 数学 l
4 王五 英语 z
5 王五 数学 l
mysql版本是多少?8+有个row_number生成序号,删除序号大于1的记录即可,SQL如下
delete from student where id in(
select id from(select name,subject,teacher,Id , row_number() over(partition by name,subject,teacher order by id) as rn from student)t where t.rn>1
)
或者 not in
delete from student where id not in(
select id from(
SELECT min(id) id FROM `student` group by name,subject,teacher)t
)
delete from student1 where id in (
select id from (
select total, id from (
select count(id) as total, min(id) as id,name, subject, teacher from student1 group by name, subject, teacher
) a where a.total > 1
) b
)
希望可以帮到你
SELECT * FROM appinfodb.student
GROUP BY name
,subject
, teacher