选择最后一行

时间:2012-12-20 09:32:50

标签: php mysql sql

我想选择每个成员的最后一行。

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)

8 个答案:

答案 0 :(得分:3)

尝试:

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

答案 1 :(得分: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只是返回第一个遇到的值。由于我们在有序子查询上使用非聚合列应用了一个组,因此第一个遇到的值就是您需要的值。

答案 2 :(得分:1)

试试这个:

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

答案 3 :(得分: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 |

答案 4 :(得分:0)

这是正确的:

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

答案 5 :(得分: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

答案 6 :(得分:0)

这很简单:

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

答案 7 :(得分:0)

SELECT max(point),id,uid FROM test GROUP BY uid ORDER BY点DESC

以这种方式使用,它将解决您的问题。