Keep only last 5 search results of user in a table
问题 I need to keep the last 5 search results of a user in a table. I wrote a script to remove other rows, but it didn't work: DELETE FROM SELECT ROW_NUMBER () OVER (ORDER BY search_time DESC) AS row_number; FROM history_user WHERE user_id = 188 WHERE row_number>5 What did I do wrong? 回答1: Proper syntax as detailed in the manual: DELETE FROM history_user h USING ( SELECT pk_id, row_number() OVER (ORDER BY search_time DESC) AS rn; FROM history_user WHERE user_id = 188 ) sub WHERE sub.rn > 5 AND h