Consider the following table:
id int primary_key
user_id int
type enum('type1', 'type2', 'type3')
with actual state:
id user_id type
1 2 type1
2 2 type1
3 2 type1
4 2 type1
5 2 type2
I'd like to write a select, which returns rows with id: 1,2,3,5; I'd like to select all rows for user_id = 2, while selecting only max 3 of each type. Row with id=4 would not be selected, because it would have already found 3 rows with type1.
One way to do it is to create 3 seperate query for each value of type and then making an UNOIN of them. Is there a better/more efficient way?