1

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?

Styles2304
  • 143
  • 1
  • 14

2 Answers2

1

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

Alma Do
  • 37,009
  • 9
  • 76
  • 105
0

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

Community
  • 1
  • 1
gpicchiarelli
  • 454
  • 6
  • 16