mysql distinct或group by

时间:2012-07-18 17:34:34

标签: mysql

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

是否可以使用该查询返回其他非独特cols?

或者我的其他解决方案:

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

这样可以正常工作但是没有正确排序。它选择了第一组。

表示例:

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

我希望它能回归:

player2 time
5        2
6        3

但它返回(使用分组):

player2 time
5        1
6        3

3 个答案:

答案 0 :(得分:4)

试试这个:

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

SELECT player2, MAX(time) AS time   
FROM logs   
WHERE player1=3  
GROUP BY player2   
ORDER BY MAX(time)