有这样一张表,表数据及结果如下:
可以看出,school_name的字段值有重复数据(Abraham Lincoln High School 和Agoura High School分别出现两次),那么如何删除这两条数据,从而只让这两个数值出现一次呢?
//删除重复记录,保存Id最小的一条
delete FROM `test` WHERE `school_name`
in
(SELECT `school_name`
FROM `test`
GROUP BY `school_name`
HAVING COUNT( * ) >1) and school_id
not in
(select min(school_id) from test group by school_id having count(* )>1)
//删除重复记录,保存Id最大的一条
delete FROM `test` WHERE `school_name`
in
(SELECT `school_name`
FROM `test`
GROUP BY `school_name`
HAVING COUNT( * ) >1) and school_id
not in
(select max(school_id) from test group by school_id having count(* )>1)
一.单个字段的操作
Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1
查看是否有重复的数据:
GROUP BY <列名序列>
HAVING <组条件表达式>
查询出:根据dname分组,同时满足having字句中组条件表达式(重复次数大于1)的那些组
count(*)与count(1) 其实没有什么差别,用哪个都可以
count(*) 与 count(列名)的区别:
count(*)将返回表格中所有存在的行的总数包括值为null的行,然而count(列名)将返回表格中除去null以外的所有行的总数(有默认值的列也会被计入)
查询全部重复的数据:
Select * From 表 Where 重复字段
In
(Select 重复字段 From 表 Group By 重复字段 Having Count(*)>1)
二.删除全部重复数据:
将上面的查询select改为delete(这样会出错的)
DELETE
FROM
dept
WHERE
dname IN (
SELECT
dname
FROM
dept
GROUP BY
dname
HAVING
count(1) > 1
)
会出现如下错误:[Err] 1093 - You can't specify target table 'dept' for update in FROM clause
原因是:更新这个表的同时又查询了这个表,查询这个表的同时又去更新了这个表,可以理解为死锁。
mysql不支持这种更新查询同一张表的操作
解决办法:把要更新的几列数据查询出来做为一个第三方表,然后筛选更新。
DELETE
FROM
dept
WHERE
dname IN (
SELECT
t.dname
FROM
(
SELECT
dname
FROM
dept
GROUP BY
dname
HAVING
count(1) > 1
) t
)
三.查询表中多余重复数据
根据depno来判断,除了rowid最小的一个
方法一
SELECT
*
FROM
dept
WHERE
dname IN (
SELECT
dname
FROM
dept
GROUP BY
dname
HAVING
COUNT(1) > 1
)
AND deptno NOT IN (
SELECT
MIN(deptno)
FROM
dept
GROUP BY
dname
HAVING
COUNT(1) > 1
)
这种写法正确,但是查询的速度太慢,可以试一下方法二
方法二
根据dname分组,查找出deptno最小的;然后再查找deptno不包含刚才查出来的,这样就查询出了所有的重复数据(除了deptno最小的那行)
SELECT *
FROM
dept
WHERE
deptno NOT IN (
SELECT
dt.minno
FROM
(
SELECT
MIN(deptno) AS minno
FROM
dept
GROUP BY
dname
) dt
)
第三种方法
SELECT
*
FROM
table_name AS ta
WHERE
ta.唯一键 <> ( SELECT max( tb.唯一键 ) FROM table_name AS tb WHERE ta.判断重复的列 = tb.判断重复的列 );
四.删除重复数据只保留一条
第一种方法:
DELETE
FROM
dept
WHERE
dname IN (
SELECT
t.dname
FROM
(
SELECT
dname
FROM
dept
GROUP BY
dname
HAVING
count(1) > 1
) t
)
AND deptno NOT IN (
SELECT
dt.mindeptno
FROM
(
SELECT
min(deptno) AS mindeptno
FROM
dept
GROUP BY
dname
HAVING
count(1) > 1
) dt
)
第二种方法
与上面查询的第二种方法对应,只是将select改为delete
DELETE
FROM
dept
WHERE
deptno NOT IN (
SELECT
dt.minno
FROM
(
SELECT
MIN(deptno) AS minno
FROM
dept
GROUP BY
dname
) dt
)
第三种方法
DELETE
FROM
table_name AS ta
WHERE
ta.唯一键 <> (
SELECT
t.maxid
FROM
(
SELECT max( tb.唯一键 ) AS maxid FROM table_name AS tb WHERE ta.判断重复的列 = tb.判断重复的列 )
t )
五.多个字段的操作(补充)
和单个字段相比,多个字段就是将group by 的字段增加为你想要的即可
DELETE
FROM
dept
WHERE
(dname, db_source) IN (
SELECT
t.dname,
t.db_source
FROM
(
SELECT
dname,
db_source
FROM
dept
GROUP BY
dname,
db_source
HAVING
count(1) > 1
) t
)
AND deptno NOT IN (
SELECT
dt.mindeptno
FROM
(
SELECT
min(deptno) AS mindeptno
FROM
dept
GROUP BY
dname,
db_source
HAVING
count(1) > 1
) dt
)
六.小结
上面的方法还有很多需要优化的地方,数据量太大的话,执行起来很慢
简单优化:
- 在经常查询的字段上加上索引
- 将*改为你需要查询出来的字段,不要全部查询出来
- 小表驱动大表用IN,大表驱动小表用EXISTS。
IN适合的情况是外表数据量小的情况,而不是外表数据大的情况.
因为IN会遍历外表的全部数据,假设a表100条,b表10000条那么遍历次数就是100*10000次,而exists则是执行100次去判断a表中的数据是否在b表中存在,它只执行了a.length次数。
至于哪一个效率高是要看情况的,因为in是在内存中比较的,而exists则是进行数据库查询操作的。