I have an SQL table with news and Unix timestamps. I would like to keep only 50 latest stories. How can I write an SQL statement to delete any number of old stories?
I ended up using two queries since MySQL5 does not yet support LIMIT in subqueries
SELECT unixTime FROM entries ORDER BY unixTime DESC LIMIT 49, 1; DELETE FROM entries WHERE unixTime < $sqlResult;
Blockquote
delete from table where id not in ( select id from table order by id desc limit 50 )
You select the identifiers of the data that you do not want to delete, and you delete all NOT IN these values ...
, , , - - , . , - , - . :
select count(*) from table;
do
delete from table order by timestamp limit result - 50;
-
, 50 TRUNCATE TABLE, . 50 .
, , :
SELECT timestampcol FROM table ORDER BY timestampcol DESC LIMIT 49,1;
:
DELETE FROM table WHERE timestampcol < ( SELECT timestampcol FROM table ORDER BY timestampcol DSEC LIMIT 49,1 )
, , , - . , , IN, 50 , , () 50 - , SQL.
IN
Perhaps not the most efficient, but this should work:
DELETE FROM _table_ WHERE _date_ NOT IN (SELECT _date_ FROM _table_ ORDER BY _date_ DESC LIMIT 50)
Source: https://habr.com/ru/post/1697633/More articles:What is your little web development? - language-agnosticJava printf functionality for collections or arrays - javaHow to get the resolution of the main monitor in Mac OS X in C ++? - c ++What is the window API for changing the screen refresh rate? - winapiКаков самый быстрый способ получить документацию для Ruby? - rubyDevelopment with a tablet, not a mouse - ergonomicsComprehensive server side validation - phpWhy does my SharePoint Navigation out of the box look like it's a memory leak - memory-leaksDoes anyone successfully use the Junction API? - apiNHibernate + JSON/Ajax Родительские/детские отношения? Почему это не работает? - jsonAll Articles