I have a table with hundreds of thousands of rows, assigned to a user. e.g.:
itemid | userid | etc
1 | 1 | etc
2 | 1 | etc
3 | 1 | etc
4 | 3 | etc
5 | 3 | etc
6 | 3 | etc
etc | etc | etc
A user can have any number of items assigned to him or her. I.e. Any number from 0 to infinity. My problem is I want an SQL query that will delete all items for each user, but keep 20. If the user has less than 20, for example, only 10 items assigned, it must keep all 10.
How do I do that?
UPDATE
If the user has 50 items, with ids 1 - 50, it must return items 30 - 50. In other words, the last 20 inserted items for that user.