mysql求游戲排名
問(wèn)題描述
問(wèn)題解答
回答1:select user_id,a.sc,min(created_at) tm from (select user_id,max(score) sc from active_gamescore group by user_id) a join active_gamescore b on a.user_id=b.user_id and a.sc=b.score group by a.user_id,a.sc order by a.sc desc,tm asc limit 20;回答2:
select t.userid,t.score from (select * from active_gamescore order by score desc,created desc) as t group by t.userid limit 20;
回答3:更新:一個(gè)人只能回復(fù)一次,很憂(yōu)傷感謝幾位抽出時(shí)間去幫我解題測(cè)試了一下(5次平均)
在1w數(shù)據(jù)量的情況下,@clcx_1315 :0.004s@伊拉克 : 0.009s@邢愛(ài)明 :0.006s我自己的 :0.016s
在20w的數(shù)據(jù)量下:@clcx_1315 :0.104s@伊拉克 : 0.141s@邢愛(ài)明 :0.165s我自己的 :0.171s
所以@clcx_1315的方法最優(yōu),十分感謝,學(xué)到了一個(gè)思路。從explain看,@clcx_1315的寫(xiě)法只做了2次全表遍歷,其他都是3次,或許這就是原因了。
===========================之前的分隔線============================琢磨了一種寫(xiě)法,但效率有待提高
select ta.user_id,ta.max_score,tb.min_timefrom (select a.user_id, max(a.score) max_score from active_gamescore a where a.active_id=’58’ group by a.user_id) tajoin (select a.user_id,a.score,min(a.created_at) min_time from active_gamescore a where a.active_id=’58’ group by a.user_id,a.score) tb on ta.user_id=tb.user_id and ta.max_score=tb.scoreorder by ta.max_score desc,tb.min_time asc limit 20回答4:
假設(shè)同一用戶(hù)下的created_at字段值不重復(fù),可以試試下面的語(yǔ)句:
SELECT t1.user_id, t1.score, t1.created_atFROM (SELECT @row_num:=IF(@prev_col1=t.user_id, @row_num+1, 1) AS row_number, t.*, @prev_col1:=t.user_idFROM (SELECT * FROM active_gamescore ORDER BY user_id, score DESC, created_at) t, (SELECT @row_num:=1, @prev_col1:=NULL) var) t1 WHERE row_number = 1ORDER BY t1.score DESC, t1.created_atLIMIT 20回答5:
SELECT yws0.user_id, yws0.score, min(create_time) AS create_timeFROM active_gamescore yws0WHERE (user_id, score) IN (SELECT yws.user_id, yws.max_scoreFROM (SELECT user_id, max(score) AS max_scoreFROM active_gamescoreGROUP BY user_id ) yws )GROUP BY yws0.user_id, yws0.scoreORDER BY SCORE DESCLIMIT 3
相關(guān)文章:
1. MySQL主鍵沖突時(shí)的更新操作和替換操作在功能上有什么差別(如圖)2. 關(guān)于mysql聯(lián)合查詢(xún)一對(duì)多的顯示結(jié)果問(wèn)題3. python中如何計(jì)算t分布的值?4. mysql在限制條件下篩選某列數(shù)據(jù)相同的值5. 數(shù)據(jù)庫(kù) - Mysql的存儲(chǔ)過(guò)程真的是個(gè)坑!求助下面的存儲(chǔ)過(guò)程哪里錯(cuò)啦,實(shí)在是找不到哪里的問(wèn)題了。6. python執(zhí)行cmd命令,怎么讓他執(zhí)行類(lèi)似Ctrl+C效果將其結(jié)束命令?7. python - scrapy url去重8. 實(shí)現(xiàn)bing搜索工具urlAPI提交9. python - Django有哪些成功項(xiàng)目?10. Python從URL中提取域名
