我有下表:
ID Category_ID Score Name
1 1 60 Name_1
2 1 50 Name_2
3 2 40 Name_3
4 3 30 Name_4
5 4 10 Name_5
假设我运行以下查询:
SELECT * from table ORDER BY Score DESC LIMIT 0, 2
这将带给我:
ID Category_ID Score Name
1 1 60 Name_1
2 1 50 Name_2
如何避免在我的结果中重复 Category_ID(尽可能多),但将 ORDER BY 分数作为第一个条件。例如,我怎样才能得到以下内容:
ID Category_ID Score Name
1 1 60 Name_1
3 2 40 Name_3 <-- Following higher score with different Category_ID
并且对于
SELECT * from table ORDER BY Score DESC LIMIT 1, 2
预期结果是:
ID Category_ID Score Name
2 1 50 Name_2
4 3 30 Name_4
SELECT * from table ORDER BY Score DESC LIMIT 2, 2
预期结果是:
ID Category_ID Score Name
5 4 10 Name_5
谢谢!