Hello.
Tell me, please, how to implement such a request. Table structure:
USERNAME | DATETIME | IP_ADDRESS user0 user0 user0 user1 user1 user1 user2 user2 user2 ... userN userN userN CREATE TABLE `log` ( `USERNAME` VARCHAR(50) NOT NULL, `DATETIME` DATETIME NOT NULL, `IP_ADDRESS` VARCHAR(15) NOT NULL ) COLLATE='utf8_general_ci' ENGINE=MyISAM; I need for each user in the table was not more than 100 records. Accordingly, if there are more, the earlier ones should be removed.
Here such request as works
delete from `log` where `USERNAME` = ? and `DATETIME` not in (select `DATETIME` from (select * from `log` order by `DATETIME` desc limit 100) s ) but I cannot repeat it for each USERNAME, as there are several thousand of them.
DATETIMEDATETIME, number int); insert into log1 (USERNAME,DATETIME, number) select USERNAME,DATETIME, if (l.USERNAME = @un, @rn: = @rn + 1, @rn: = 1 + least (0, @un: = l.USERNAME )) as number from log l, (select @rn: = 0, @un = '_') zz order by USERNAME,DATETIMEdesc; delete log from log join log1 on log.USERNAME = log1.USERNAME and log.DATETIME = log1.DATETIME where log1.number> 100; Of course, everything can be pushed into one request, but it will look awful. - alexlz