---
title: "mysql临时表技巧"
author: "javafan_303"
date: "2011-01-25T11:23:56Z"
tags: ["MySQL"]
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2011-01-25T11:24:17Z"
id: 894872
url: "https://cailin.iteye.com/blog/894872"
---

给定一个user表，包含id和uid两列，其中uid列可能有重复，要求找出重复的记录，并删掉多余的记录，使得对于uid重复的记录只保留id最小的记录

```
drop table if EXISTS `tmp`;
create table tmp as select min(id) as id,uid from user GROUP by uid HAVING count(uid)>1;
delete from user where id not in (select id from tmp)  and uid in(select uid from tmp); 
drop table tmp;
commit;
```