简体   繁体   中英

Select the highest last row

I want to select the highest last row of each member.

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

So I want to select the ID, UID and POINT where the point is the higest of each user. The result should be:

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

I tried with this:

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

AND

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

But I got the some wrong result:

4(3), 2(2), 1(1)

尝试:

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

This query will select the highest points for each user:

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

and this will select the maximum id where the user has the maximum points:

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

and this is the final query that you need:

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)

that shows:

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

this will also give the same result, and looks simpler:

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

I think that it will always work, but it's not documented!

A little explanation for the last query: MySql allows you to select nonaggregated fields in a group by query. Here we are grouping by uid but selecting all columns: the documentation says that the values of the nonaggregated columns will be undetermined (it can be any value inside the group) but in fact MySql just returns the first encountered value. And since we are applying a group by with nonaggregated columns on an ordered subquery, the first encountered value is what you need.

Try this :

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

Query:

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))

Result:

| 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 |

这是正确的:

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

Query:

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

很简单:

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

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

use in this way, it wil solve your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM