Question

I'm trying to delete all entries except the 25 most recent based on the timestamp. I tried this:

DELETE FROM `chat` WHERE `IndexNo` NOT IN(SELECT `IndexNo` FROM `chat` ORDER BY `Timestamp` DESC LIMIT 25)

That generates this error:

#1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

Is there a way to accomplish this?

Était-ce utile?

La solution

You can not rely on LIMIT in IN/ALL/ANY/SOME subqueries, but instead you can use multi-delete syntax:

DELETE 
  `chat` 
FROM 
  `chat` 
    LEFT JOIN (SELECT `IndexNo` FROM `chat` ORDER BY `Timestamp` DESC LIMIT 25) AS `selected`
      ON `chat`.`IndexNo`=`selected`.`IndexNo` 
WHERE 
  `selected`.`IndexNo` IS NULL;

-according to manual, this will work since MySQL 5.0

Autres conseils

It is a more general issue about subquery I guess. Try to take a look here: Problem with LIMIT & IN/ALL/ANY/SOME subquery

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top