2015-10-02 28 views
1

大家好我有一個已經user_id說明表和自動遞增標識如何GROUP BY和使用秩序2個不同的ID

user_id|points|id 
    3432 |400 |1 
    3431 |99 |2 
    3333 |340 |3 
    4671 |34 |4 
    9911 |700 |5 
    3432 |100 |6 
    3333 |841 |7 

我的目標是創建一個將責令基於較高的USER_ID的sql分但考慮考慮的最後一個自動遞增的編號user_id.I的需要我的輸出是這樣

user_id|points 
3333 |841 
9911 |700 
3432 |100 
3431 |99 
4671 |34 

但目前它只能挑USER_ID和第一點意思的

代替

做 3432 | 400

下面

SELECT * From points_table where points < 3000 GROUP BY user_id ORDER BY points DESC. 

回答

2

檢查MySQL中,你只是想最後一條記錄,然後對他們進行排序。這裏有一種方法:

select pt.* 
from points_table pt 
where pt.id = (select max(pt2.id) from points_table pt2 where pt2.user_id = pt.user_id) 
order by pt.points desc; 
+0

,有趣的是它的作品謝謝。 –