SQL - insert and limit the number of entries
I have a table that represents a user's entries:
User_ID | Entry_ID
Now I want to store only the last 20 entries (for example).
What would be the best way to insert new entries while keeping the maximum number of entries per user at 开发者_如何学运维20 (new entries will replace oldest entries) ?
I need to be able to insert also a list of items to replace the old ones (if possible, to avoid multiple SQL requests)
Sounds like a job for a trigger...
Add a statement-level after insert trigger, which deletes rows like:
delete from yourtbl
where yourid not in (
select yourid from yourtbl order by yourid desc limit 20
)
精彩评论