Remove duplicates using only a MySQL query?

后端 未结 7 1181
死守一世寂寞
死守一世寂寞 2020-11-27 07:55

I have a table with the following columns:

URL_ID    
URL_ADDR    
URL_Time

I want to remove duplicates on the URL_ADDR column

相关标签:
7条回答
  • 2020-11-27 08:24

    This will leave the ones with the highest URL_ID for a particular URL_ADDR

    DELETE FROM table
    WHERE URL_ID NOT IN 
        (SELECT ID FROM 
           (SELECT MAX(URL_ID) AS ID 
            FROM table 
            WHERE URL_ID IS NOT NULL
            GROUP BY URL_ADDR ) X)   /*Sounds like you would need to GROUP BY a 
                                       calculated form - e.g. using REPLACE to 
                                      strip out www see Daniel's answer*/
    

    (The derived table 'X' is to avoid the error "You can't specify target table 'tablename' for update in FROM clause")

    0 讨论(0)
提交回复
热议问题