简体   繁体   中英

mysql distinct or group by

SELECT DISTINCT(player2) FROM logs 
WHERE player1=3 
ORDER BY time DESC

Would it be possible to return other non-distinct cols with that query?

Or my other solution:

SELECT * FROM logs 
WHERE player1=3 
GROUP BY player2 ORDER BY time DESC

This works fine but it does not order properly. It picks the first one of the group.

Table example:

player1 player2 time
3        5        1
3        5        2
3        6        3

I would expect it to return:

player2 time
5        2
6        3

But it returns (with group by):

player2 time
5        1
6        3

Try this:

SELECT player2, MAX(time) AS time
  FROM logs 
 WHERE player1=3 
 GROUP BY player2 
 ORDER BY time DESC
SELECT player2, max(`time`) as max_time
FROM logs 
WHERE player1 = 3 
GROUP BY player2 
SELECT player2, MAX(time) AS time   
FROM logs   
WHERE player1=3  
GROUP BY player2   
ORDER BY MAX(time) 

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