EchoFool Posted June 11, 2010 Share Posted June 11, 2010 Hey, I have a query which is meant to delete all rows which are less than the last 31 ids with this WHERE clause. As i only display the most latest 31 rows the rows that are any older might aswell be deleted but it won't work for me. The error i get is: You can't specify target table 'messages' for update in FROM clause DELETE FROM messages WHERE id < (SELECT MAX(id) FROM messages) - 31 Hope you can help! Quote Link to comment https://forums.phpfreaks.com/topic/204509-delete-query-not-working/ Share on other sites More sharing options...
jskywalker Posted June 12, 2010 Share Posted June 12, 2010 do it in 2 steps $max = mysql_query('SELECT MAX(id) FROM messages'); $max = $max -31; mysql_query('DELETE FROM messages WHERE id < '.$max); Quote Link to comment https://forums.phpfreaks.com/topic/204509-delete-query-not-working/#findComment-1071198 Share on other sites More sharing options...
EchoFool Posted June 12, 2010 Author Share Posted June 12, 2010 Thought that might be the way to do - i was hopeing a single query could. But i will use your suggestion. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/204509-delete-query-not-working/#findComment-1071257 Share on other sites More sharing options...
Mchl Posted June 12, 2010 Share Posted June 12, 2010 DELETE FROM messages WHERE id NOT IN (SELECT id FROM messages ORDER BY id DESC LIMIT 31) Quote Link to comment https://forums.phpfreaks.com/topic/204509-delete-query-not-working/#findComment-1071259 Share on other sites More sharing options...
EchoFool Posted June 12, 2010 Author Share Posted June 12, 2010 Hi Mchl Thanks for that - i tried it but i get: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery' Never had that before - v.odd error =/ any ideas why ? Quote Link to comment https://forums.phpfreaks.com/topic/204509-delete-query-not-working/#findComment-1071261 Share on other sites More sharing options...
Mchl Posted June 12, 2010 Share Posted June 12, 2010 Oh well... MySQL does not support may features... This should work DELETE messages FROM messages LEFT JOIN ( SELECT id FROM messages ORDER BY id DESC LIMIT 31 ) AS sq USING (id) WHERE sq.id IS NULL Quote Link to comment https://forums.phpfreaks.com/topic/204509-delete-query-not-working/#findComment-1071275 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.