Different kind of LIMIT

This is probably a dumb question, but I’ll put it forth anyway. Is there a routine or easy way to limit the number of items in a group? What I want to do is limit the number of items in a group to no more than a certain number. For instance, the last 10 times someone logged in.

I’m thinking of a routine that takes in field1, field2, # limit, and then an optional keyword of {FIRST,LAST} and maybe an optional WHERE clause. So in an example, the routine would take in:


and the routine would find the number of times uid 12345 logged in. If it’s less than or equal to 10, leave it alone. If it’s greater than 10, delete it so it gets to 10, deleting the oldest records first.

This is not something that could be done with a trigger (ie, on insert of a new login, check to see how many logins there are, and if there are 10 delete the first (oldest) one) because in our case, it’s done when we take away privileges from someone. We pare down their friends list, or reduce the # of images they’re allowed, etc. We usually do a loop with code, but I’d rather have a stored routine do it. (bonus points if I can use it in MySQL 4.1.12).

Comments are closed.