DELETE FROM1 f 
WHERE
 ( f.字段1,f.字段2 ) IN ( SELECT 字段1,字段2 FROM1 GROUP BY 字段1,字段2 HAVING count(*) > 1 )
 AND 
 id NOT IN ( SELECT min(id) FROM1 GROUP BY 字段1,字段2 HAVING count(*) > 1 )

按照上面这种模式编写sql;mysql可能出现一下错误

You can’t specify target table for update in FROM clause 

这个错误的意思是不能在同一个sql语句中,先select同一个表的某些值,然后再update这个表。

把select在用括号包一层即可,例如

DELETE 
FROM
	custom_position_view 
WHERE
	( position_id, view_user_id ) IN ( SELECT position_id, view_user_id FROM ( ( SELECT position_id, view_user_id FROM custom_position_view GROUP BY position_id, view_user_id HAVING count(*) > 1 ) ) t ) 
	AND id NOT IN (
	SELECT tt.idx FROM ( SELECT min( id ) idx FROM custom_position_view GROUP BY position_id, view_user_id HAVING count(*) > 1 ) tt 
	)
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐