Suppose you have a table like:
create table user_news (
user_id int unsigned not null,
article_id int unsigned not null,
article_date timestamp,
primary key (user_id, article_id),
key (user_id, article_date));
How can you ensure that every user_id has no more than 30 rows? That is, how can you delete all the rows ordered by article_date offset by 30 per user_id?
Note: The queries below use
>= 2since the test dataset is small. Change it to>= 30for your solution.This is the DELETE statement.
Based on this test table
And here is the inner SELECT query on its own to see what is happening