开发者

SQL delete all rows except some ones

开发者 https://www.devze.com 2023-03-27 03:16 出处:网络
I have a tab开发者_StackOverflow社区le with the following columns (of urls): [id,url,visited,timestamp]

I have a tab开发者_StackOverflow社区le with the following columns (of urls):

 [id,url,visited,timestamp]
 Types:[int,string,int,long]

I want to:

Delete all urls except 10 unvisited priorizing higher timestamp (or delete all if all are visited for example)

Its posible to do that in a single query? Anyway whats the best query (queries) for doing it?

Thanks in advance


I don't think TOP works in sqlite -- need to use LIMIT

DELETE FROM mytable WHERE id NOT IN ( 
   SELECT id FROM mytable  
   WHERE visited = false 
   ORDER BY timestamp DESC
   LIMIT 10  
   )  


DELETE FROM tableofDeletion
WHERE
  -- Delete all items not in the following select
  -- ordered by the timestamp so we can get the top 10 
  id NOT IN (SELECT id 
             FROM tableofDeletion
             WHERE 
                 visited = 0 -- false
             ORDER BY timestamp DESC
             LIMIT 10)

I think this delivers what you're looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消