需求:删除表中重复的数据,保留一条记录,其余的删除
下面这条SQL语句的功能就是查询出单表中重复的数据并删除
DELETE FROM wz_outpatient WHERE id in(SELECT a.id FROM
(SELECT id FROM wz_outpatient WHERE (SFXMDM, KSDM,RQ,HJJE )
IN ( SELECT SFXMDM, KSDM,RQ,HJJE FROM wz_outpatient GROUP BY SFXMDM, KSDM,RQ,HJJE HAVING count( * ) > 1 )
AND id NOT IN ( SELECT min( id ) FROM wz_outpatient
GROUP BY SFXMDM, KSDM,RQ,HJJE HAVING count( * ) > 1 )) a);
咱们现在开始一步步的拆解这段语句
-
step1
SELECT SFXMDM, KSDM,RQ,HJJE FROM wz_outpatient GROUP BY SFXMDM, KSDM,RQ,HJJE HAVING count( * ) > 1
这最里层的SQL语句中,咱们根据一些条件查询了wz_outpatient
表中的几个字段值,并加上了Having
筛选了数据重复条目大于1的结果。
-
step2
SELECT id FROM wz_outpatient WHERE (SFXMDM, KSDM,RQ,HJJE )
IN ( 上例)
AND id NOT IN ( SELECT min( id ) FROM wz_outpatient
GROUP BY SFXMDM, KSDM,RQ,HJJE HAVING count( * ) > 1 )
这个SQL语句里面有两个查询条件
- 将上例的结果作为这次语句的
in
条件查询id
-
id
在第二个条件中将重复数据查询返回了之后,筛选出除了id
最小之外的其他数据
PS: 也可用MAX()
功能查询除了id
最大的之外的结果集
这一步骤的结果就是查询了该表中的除了最小id
之外的重复数据
-
step3
SELECT a.id FROM (上例) a
逻辑上其实可以忽略这一步骤直接拼接delete
语句就可删除重复数据并保留一条id
最小的数据.但是在MySQL中执行的话,会报如下错误
大致意思就是不可在直接查询表又对表进行更新
所以我们就加了step3,将查询结果做出中间过渡表a
-
step4
DELETE FROM wz_outpatient WHERE id in(SELECT a.id FROM
(SELECT id FROM wz_outpatient WHERE (SFXMDM, KSDM,RQ,HJJE )
IN ( SELECT SFXMDM, KSDM,RQ,HJJE FROM wz_outpatient GROUP BY SFXMDM, KSDM,RQ,HJJE HAVING count( * ) > 1 )
AND id NOT IN ( SELECT min( id ) FROM wz_outpatient
GROUP BY SFXMDM, KSDM,RQ,HJJE HAVING count( * ) > 1 )) a);
执行SQL,亲测有效
如有疑问可评论,喜欢请like
一下