我的表结构如下:
Name email score time
Hello abc@gmail.com 100 15
Hello abc@gmail.com 58 10
Test def@gmail.com 100 12
Stack xyz@gmail.com 90 20
Test def@gmail.com 50 40
使用选择查询
$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 abc@gmail.com 100 15
Test def@gmail.com 100 12
Stack xyz@gmail.com 90 20
我感到困惑的是,如果两个用户得分相同,我想根据最短的时间对结果进行排序,因为“测试”用户在12秒内得分为100,所以应该先得分。
答案 0 :(得分: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
答案 1 :(得分:1)
试试这个mysql查询
$q="SELECT name, MAX(score) as score
,email FROM users GROUP BY email
ORDER BY MAX(score) DESC,time LIMIT 10 ";