2012-12-20 155 views
4

我想選擇每個成員的最後一行。選擇最後一行

ID  UID  POINT  DATE   TIME 

1  1   5  2012-11-29  11:29:03  
2  2  10  2012-11-29  11:38:12  
3  1  10  2012-12-02  05:15:01  
4  3   5  2012-12-02  09:51:34  
5  2   5  2012-12-02  12:14:14  
6  3   5  2012-12-04  12:18:30 
7  1   5  2012-12-05  06:00:51 

所以我想選擇ID,UID和POINT,其中點是每個用戶的最高點。 結果應該是:

ID  UID  POINT  DATE   TIME 

2  2  10  2012-11-29  11:38:12  
3  1  10  2012-12-02  05:15:01  
6  3   5  2012-12-04  12:18:30 

我試着用這樣的:

SELECT distinct uid, point, id FROM `test` 
GROUP By uid ORDER BY date DESC, time DESC 

SELECT id, uid, point FROM `test` 
GROUP BY uid ORDER BY date DESC, time DESC 

但我得到了一些錯誤的結果:

4(3), 2(2), 1(1) 
+0

你能不能在適當的列ID中顯示你的預期結果?是不是'max'後跟'max()'? – bonCodigo

+0

我編輯的開幕帖子(補充說:結果應該是:...) – Gery

+0

所以你需要你的數據採取日期,時間考慮? – bonCodigo

回答

3

這個查詢爲每個用戶選擇的最高點:

select uid, max(`points`) 
from members 
group by uid 

,這將選擇用戶擁有最大分數的最大ID:

select uid, max(id) 
from members 
where (uid, `points`) in (select uid, max(`points`) 
          from members 
          group by uid) 
group by uid 

,這是你所需要的最終查詢:

select members.* 
from members 
where (uid, id) in (
    select uid, max(id) 
    from members 
    where (uid, `points`) in (select uid, max(`points`) 
          from members 
          group by uid) 
    group by uid) 

顯示:

ID UID POINT DATE  TIME 
2 2 10  2012-11-29 11:38:12  
3 1 10  2012-12-02 05:15:01  
6 3 5  2012-12-04 12:18:30 

這也會產生相同的結果,看上去簡單:

SELECT s.* 
FROM 
    (SELECT members.* 
    FROM members 
    ORDER BY uid, points desc, id desc) s 
GROUP BY uid 

我認爲它會一直工作,但它'沒有記錄!

對最後一個查詢的一點解釋:MySql允許您通過查詢來選擇組中的非聚集字段。在這裏,我們按uid進行分組,但選擇所有列:文檔說明非聚集列的值將不確定(它可以是組內任意值),但實際上MySql只是返回遇到的第一個值。而且,由於我們在有序子查詢中應用了一個非聚集列的組,所以遇到的第一個值就是您需要的值。

+0

似乎可以在幾個名稱更改後確定 – Gery

+0

我喜歡簡單但它太簡單工作,但它看起來工作:)謝謝 – Gery

+0

@Gery我編輯了一點我的答案,解釋爲什麼最後一個查詢沒有記錄,以及爲什麼我相信它將始終工作! – fthiella

1

試試這個:

SELECT id, uid, point FROM `test` 
GROUP BY uid 
ORDER BY point DESC, date DESC, time DESC 
+0

我得到了錯誤的結果,在uid爲3的情況下,ID將會是4的insted 6 – Gery

3

嘗試:

SELECT id, uid, MAX(point) FROM `test` GROUP BY uid ORDER BY date DESC, time DESC 
+0

給出錯誤結果。 –

+0

我只得到1行(但我有3個用戶,所以應該是3行):id/uid/MAX(point):4/3/5(增加:結果應該是:...格式類型在我的開頭張貼)或更改爲3錯誤也是錯誤的結果 – Gery

+0

試過查詢在我的文章和工作。我確實在TIMESTAMP列中結合了DATE&TIME列。 SELECT id,uid,MAX(point)FROM'test' GROUP BY uid ORDER BY date DESC – ennovativemedia

0

這是正確的:

SELECT ID, UID, MAX(POINT) FROM `test` GROUP BY UID ORDER BY DATE DESC, TIME DESC 
+0

相同:在UID爲3的情況下,ID應該是6,因爲最新插入的行是6(點是相同的) – Gery

0

查詢:

SELECT x.* 
FROM (SELECT p.* 
FROM points p 
ORDER BY uid, point desc, id desc) as x 
GROUP BY x.uid 
; 

Reusults:

ID UID  POINT DATE        TIME 
3 1  10  December, 02 2012 00:00:00+0000  January, 01 1970 05:15:01+0000 
2 2  10  November, 29 2012 00:00:00+0000  January, 01 1970 11:38:12+0000 
6 3  5  December, 04 2012 00:00:00+0000  January, 01 1970 12:18:30+0000 
+0

在哪裏UID是3的ID應該是6,因爲最新插入的行是6(點是相同的) – Gery

+0

@Gery只是不符合你更新的預期結果。你最好用fthiela和justin。 +1的問題。 – bonCodigo

1

查詢:

SQLFIDDLEEXample

SELECT t1.* 
FROM Table1 t1 
WHERE t1.ID = (SELECT MAX(t3.ID) 
       FROM Table1 t3 
       WHERE t1.UID=t3.UID 
       AND t3.POINT=(SELECT MAX(t2.POINT) 
           FROM Table1 t2 
           WHERE t2.UID = t3.UID)) 

結果:

| ID | UID | POINT |       DATE |  TIME | 
----------------------------------------------------------------- 
| 2 | 2 | 10 | November, 29 2012 00:00:00+0000 | 11:38:12 | 
| 3 | 1 | 10 | December, 02 2012 00:00:00+0000 | 05:15:01 | 
| 6 | 3 |  5 | December, 04 2012 00:00:00+0000 | 12:18:30 | 
+0

看起來也可以... – Gery

0

就算這樣簡單:

SELECT * FROM (SELECT * FROM `points` ORDER BY point DESC) AS `t1` GROUP BY uid; 
+0

不是那麼容易:) – Gery

+0

在那裏,看起來很簡單的權利! – andho

+0

'訂單通過uid,點desc,id desc'就像我的回答是更好的:)它會工作,我使用這個技巧很多,即使它沒有記錄 – fthiella

0

SELECT MAX(點),ID,UID來自測試 GROUP BY UID ORDER BY點DESC

使用這種方式,它將解決你的問題。