how can I delete duplicates in SQLite?

后端 未结 2 1775
遇见更好的自我
遇见更好的自我 2020-12-09 10:30

I have a SQLite DB where the statement:

SELECT messdatum, count(*) as anzahl 
from lipo 
GROUP BY Messdatum 
ORDER BY anzahl desc;

results

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 11:20

    SQLite has a special column, ROWID created on every table by default (you can switch it off using the WITHOUT ROWID modifier, but be very sure before doing so).

    This means that we can identify specific rows within sets of duplicates, for example, finding the first entry for a value:

    SELECT messdatum, MIN(ROWID) FROM lipo
    

    So one way to remove duplicates might be this:

    DELETE FROM lipo
    WHERE rowid NOT IN (
      SELECT MIN(rowid) 
      FROM lipo 
      GROUP BY messdatum
    )
    

提交回复
热议问题