I have searched a lot of different posts about this and i didn't find something that works for me.
This Using LIMIT within GROUP BY to get N results per group? and http://www.sqlines.com/mysql/how-to/get_top_n_each_group didn't work for me.
I have a simple MySQL Table with the following columns
id , package, user_id, date
I want to perform a query in which i will get
X Rows / user_id WHERE date > Number Order By date ASC
In short, we want to perform a Group By user_id
with LIMIT of X rows / group
but have in mind the statement using the date column
Example Full Table:
id , package, user_id, date
1, full, 1 , 1447003159
2, full, 1 , 1447003055
3, full, 1 , 1447002022
4, full, 1 , 1447001013
5, full, 1 , 1447000031
6, mid, 2 , 1447003159
7, mid, 2 , 1447003055
8, mid, 2 , 1447002022
9, mid, 2 , 1447001013
10, mid, 2 , 1447000031
From the above table we want to Select only 2 rows / user_id but where date >= 1447000031 (But make ORDER BY date ASC
first)
Expected Output:
4, full, 1 , 1447001013
3, full, 1 , 1447002022
9, mid, 2 , 1447001013
8, mid, 2 , 1447002022