2015-10-16 61 views
1

我的表结构如下:排序分数然后通过时间

Name email   score time 
Hello [email protected] 100 15 
Hello [email protected] 58 10 
Test [email protected] 100 12 
Stack [email protected] 90 20 
Test [email protected] 50 40 

使用select查询

$q="SELECT name, MAX(score) as score ,email FROM users GROUP BY email ORDER BY MAX(score) DESC LIMIT 10"; 

下面的结果产生。

Name email   score time 
Hello [email protected] 100 15 
Test [email protected] 100 12 
Stack [email protected] 90 20 

我很困惑,如果两个用户有相同的分数,我想,基于最低时的结果进行排序,因为“测试”的用户在12秒内连得100,它应该是第一位的。

+0

可这对您有所帮助http://stackoverflow.com/questions/514943/php-mysql-order-by-两列 –

+0

推荐:http://stackoverflow.com/questions/5417980/mysql-sql-specific-item-to-be-first-and-then-to-sort-the-rest-of-the-items –

回答

2
SELECT name, MAX(score) as score, email 
FROM users GROUP BY email 
ORDER BY MAX(score) DESC, time ASC LIMIT 10; # note how multiple column ordering is made 

了解更多:SQL multiple column ordering

+0

谢谢亲爱的。工作正常。 – Roger

1

试试这个MySQL查询

$q="SELECT name, MAX(score) as score 
     ,email FROM users GROUP BY email 
     ORDER BY MAX(score) DESC,time LIMIT 10 "; 
相关问题