2012-07-06 39 views
1
id | userid | total_points_spent 
1 | 1  | 10 
2 | 2  | 15 
3 | 2  | 50 
4 | 3  | 5 
5 | 1  | 15 

通過上述表中刪除重複的,我首先要去除的userid重複保持行與最大total_points_spent,就像這樣:從一列保持全行

id | userid | total_points_spent 
3 | 2  | 50 
4 | 3  | 5 
5 | 1  | 15 

然後會喜歡總結total_points_spent的值,這將是容易的部分,導致70

+2

什麼是你的mysql查詢? – Jocelyn 2012-07-06 00:53:17

+0

你是什麼意思? – Tyilo 2012-07-06 01:00:06

+0

2個表中顯示的數字似乎是mysql查詢的結果。如果確實如此,你能告訴我們這個mysql查詢的代碼嗎?否則,你有什麼嘗試? – Jocelyn 2012-07-06 01:01:54

回答

2

我不確定你的意思是「刪除」是刪除或選擇。以下是分別查詢僅選擇最大totalpointspend記錄。

SELECT tblA.* 
    FROM (SELECT userid, MAX(totalpointspend) AS maxtotal 
      FROM tblA 
      GROUP BY userid) AS dt 
INNER JOIN tblA 
    ON tblA.userid = dt.userid 
    AND tblA.totalpointspend = dt.maxtotal   
ORDER BY tblA.userid