where c.name in (select cc.name from co_jobinformation cc group by cc.name having count(cc.name) > 1)
and rowid not in (select min(rowid) from co_jobinformation e group by e.name having count(e.name )>1)
之前在oracle数据库中能删除重复数据并且能保留一条唯一数据,但是相同的MySQL中就不行,
mysql有个特性,对于表进行修改,删除操作,子查询不能和外层的查询的表一样,所以在加个select就可以了
DELETE tt.*
FROM t_user tt --这是操作的表
WHERE tt.username --这是用户名重复的数据
IN(
SELECT cc.username
FROM (
SELECT b.*
FROM t_user b
) cc
GROUP BY cc.username
HAVING COUNT(cc.username) >1
)
AND tt.id --这里是保留id最小的一条,应该比较容易看懂.
NOT IN(
SELECT MIN(e.id)
FROM(
SELECT ee.*
FROM t_user ee) e
GROUP BY e.username
HAVING COUNT(e.username )>1)
)
)
刚才网上找到一种写法
select a.* from jc_informationpublic a
where a.id !=
(
select max(b.id) from jc_informationpublic b
where a.unit_name = b.unit_name
)
AND id NOT IN (SELECT min(id) FROM jc_informationpublic c GROUP BY c.unit_name HAVING COUNT(c.unit_name)>1)
也能实现效果,但是据说效率不高.
我写的有点丑,但是我想要的结果实现了,就是把上面子查询里面的表换成select查询,还请高手留下优质SQL,感激不尽!