---
title: "sql删除重复记录"
author: "cfyme"
date: "2015-09-16T14:46:35Z"
tags: []
categories: ["数据库"]
slug: "sql"
draft: false
lastmod: "2015-09-16T14:46:35Z"
id: 2243941
url: "https://cfyme.iteye.com/blog/2243941"
---

mysql 删除重复记录，保留最新的一条：

```
delete from wechat_exchange_address
    where open_id in (select  open_id from  (select open_id from  wechat_exchange_address  a  group by a.open_id having count(a.open_id) > 1) as a) 
    and id not in (select id from (select max( id) id from wechat_exchange_address group by  open_id having count( open_id)>1) as b);
```