create table posts ( id int not null auto_increment primary key, user_id int, post_date date ) engine = myisam; insert into posts (user_id,post_date) values (1,'2011-03-02'), (1,'2011-04-10'), (1,'2011-11-13'), (2,'2011-03-02'), (2,'2011-03-02'), (3,'2011-01-01'), (3,'2011-01-02'), (3,'2011-01-03'); select * from ( select posts.*, @num := if(@user_id = user_id, @num + 1, 1) as rownum, @user_id:=user_id as uid from posts,(select @user_id := 0, @num := 1) as t order by user_id asc, post_date asc) as tab where rownum = 3 order by post_date limit 1
This is an example when I find the first user that reaches 3 posts. Hope this helps you.
source share