删除id重复且name为空的数据,uuid为key
表如下
第一:找到重复的id
select id from z group by z.id having count(z.id)>1
第二:找到name为空的uuid
select z.uuid from z where z.name
is null
第三:删除
delete from z
where z.id in (select * from (select id from z GROUP BY z.id having count(z.id)>1) b)
and z.uuid in (select * from (select z.uuid from z where z.name
is null) a);
结果:
错误示范:
delete from z
where z.id in (select id from z GROUP BY z.id having count(z.id)>1)
and z.uuid in (select z.uuid from z where z.name
is null) ;
报错:[Err] 1093 - You can’t specify target table ‘z’ for update in FROM clause:不能对进行查询操作的表进行update操作,也就说我们的where条件中进行了子查询,并且子查询也是针对需要进行update操作的表的,mysql不支持这种查询修改的方式。
因此需要select * from 查询结果 别名a
SELECT * FROM (SELECT z.uuid FROM z WHERE z.name
is null) a