mysql刪除重復記錄並且只保留一條


Select 重復字段 From 表 Group By 重復字段 Having Count(*)>1

count(*)與count(列名)的區別:

    count(*)將返回表格中所有存在的行的總數包括值為null的行,然而count(列名)將返回表格中除去null以外的所有行的總數(有默認值的列也會被計入)

 

    
delete from logisticscodecheckout_copy1 ta
where exists
(
select tb.Id from logisticscodecheckout_copy1 tb
where ta.LogisticsCode = tb.LogisticsCode
and ta.Id<tb.Id 
)        
and ta.LogisticsCode='003000199344'

## You can't specify target table 'logisticscodecheckout_copy1' for update in FROM clause

delete from tbl where id in
(
    select a.id from
    (
        select max(id) id from tbl a where EXISTS
        (
            select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
        )
        group by tac
    ) a
)

 
--執行成功
delete from logisticscodecheckout_copy1 where Id in
(
    select a.Id from
    (
        select ta.Id from logisticscodecheckout_copy1 ta
where exists
(
select tb.Id from logisticscodecheckout_copy1 tb
where ta.LogisticsCode = tb.LogisticsCode
and ta.Id<tb.Id 
)        
    ) a
)

 

  

 

 

DELETE
FROM
    dept
WHERE
    deptno NOT IN (
        SELECT
            dt.minno
        FROM
            (
                SELECT
                    MIN(deptno) AS minno
                FROM
                    dept
                GROUP BY
                    dname
            ) dt
    )

  

來源 :

https://www.jb51.net/article/60926.htm

https://blog.csdn.net/n950814abc/article/details/82284838 

 

SQL 刪除重復記錄方法:https://www.cnblogs.com/shy1766IT/p/5185719.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM