How to delete last 10 records in sqlite

后端 未结 3 490
闹比i
闹比i 2021-01-02 21:48

In Sqlite, can I know how to delete last 10 records? I\'ve wrote following coding but seems not working at all.

delete from tb_news where newsid = (SELECT ne         


        
相关标签:
3条回答
  • 2021-01-02 22:29

    Did you tried?

    delete from tb_news where newsid IN (SELECT newsid from tb_news order by newsid asc limit 20)
    

    I don't know your table structure, but maybe, it should be DESC instead of ASC. I mean DESC will give you the biggest IDs (and so, the latest).

    0 讨论(0)
  • 2021-01-02 22:35

    You can use

     delete from tb_news where newsid IN 
    (SELECT newsid from tb_news order by newsid desc limit 10)
    
    0 讨论(0)
  • 2021-01-02 22:36

    Change your SQL statement to the below.

    delete from tb_news where newsid IN (SELECT newsid from tb_news order by newsid DESC limit 20)
    

    Side note: sqllite may not support LIMIT in the sub query.

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