UPDATE student b SET b.sname = 'dd' WHERE b.id = (SELECT a.id FROM student a WHERE a.id = '3')
Mysql中根据条件(表A中的字段)操作表A中的数据时是不可以的
所以借助临时表来删除/更新重复的数据,原理就是删除每组重复数据中除id值最大的其他记录
select id,name from student
1、根据特定条件查询出每组重复数据中id最大的一条记录
SELECT MAX(id) id,name FROM student GROUP BY name HAVING COUNT(name) > 1
2、将id值和查重条件添加到临时表maxids
Create table maxids
SELECT MAX(id) id,name FROM student GROUP BY name HAVING COUNT(name) > 1
3、根据查重条件查询出重复数据中id值不为最大id的所有记录的id,此处一定要注意使用去重条件去查询,不然会将其他不重复的数据查询出来
SELECT id FROM student a
WHERE EXISTS ( SELECT id FROM maxids WHERE a.id != id AND a.name = `name`)
4、将3中查出的记录添加到临时表repeatids
Create table repeatids
SELECT id FROM student a
WHERE EXISTS ( SELECT id FROM maxids WHERE a.id != id AND a.name = `name`)
5、根据临时表repeatids中的id值删除重复数据
Delete from student where id in (select id from repeatids)
6、删除临时表
Drop table maxids
Drop table repeatids
7、去重之后结果
select id,name from student