I have the following table
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| ID | int(11) unsigned | NO | PRI | NULL | auto_increment |
| userid | int(10) unsigned | NO | MUL | NULL | |
| logintime | int(10) unsigned | NO | | NULL | |
| loginIP | int(4) unsigned | NO | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
I insert a new row in this table every time a user logs in, which contains their login time, IP address etc...
Now I want to limit the number of rows to only 10 per user! i.e. if there's 10 rows for userid 5, already and I'm inserting a new one, I need to delete the oldest one of them before the insert.
is there a way to write a SQL statement that DELETE
s all entries of a userid, except for the recent 10 ones. I know how to use LIMIT
for a SELECT
, but I cannot see how I can implement the LIMIT in a DELETE
.