排序赢家。
你将如何获得PHP / MYSQL竞赛的胜利者。
总价值是决定赢家的因素。 但是在平局中赢得最高价值的那个。 如果仍然画比较第二个值先比较最佳结果
数据库表具有playerid,结果字段。
Playerid result 1 100 1 80 1 80 2 100 2 80 2 40 2 40 3 160 3 100 4 130 4 130 5 270结果列表
Player1 100,80,80 Player2 100,80,40,40 Player3 160,100 Player4 130,130 Player5 270想要结果
Player5 Player3 Player4 Player1 Player2Sorting a winner.
How would you get the winner of a competition PHP/MYSQL.
Total value is the factor that decides the winner. But on a draw the one with the highest value wins. Compare best result first if still draw compare the second value ..
DB table has playerid, result fields.
Playerid result 1 100 1 80 1 80 2 100 2 80 2 40 2 40 3 160 3 100 4 130 4 130 5 270List of results
Player1 100,80,80 Player2 100,80,40,40 Player3 160,100 Player4 130,130 Player5 270WANTED RESULT
Player5 Player3 Player4 Player1 Player2最满意答案
只需使用group by和ORDER BY SUM(result) DESC, COUNT(*) :
SELECT PlayerId, SUM(result) res FROM resulttbl GROUP BY PlayerId ORDER BY res DESC,COUNT(*)我猜想你希望那些入场次数较少(=较少的比赛?)的球员排在第一位,所以我增加了第二个排序标准。
或者你可以做
select PlayerId from resulttbl GROUP BY PlayerId ORDER BY sum(result) DESC, COUNT(*)如果你不想列出结果的总和。
你可以在这里找到一个工作演示: http : //rextester.com/GTHMG11363
Simply select with group by and ORDER BY SUM(result) DESC, COUNT(*):
SELECT PlayerId, SUM(result) res FROM resulttbl GROUP BY PlayerId ORDER BY res DESC,COUNT(*)I added the second sorting criterion as I am guessing that you want those players with fewer entries (=fewer games?) listed first.
Or you can do
select PlayerId from resulttbl GROUP BY PlayerId ORDER BY sum(result) DESC, COUNT(*)if you don't want the sum of the results listed.
You can find a working demo here: http://rextester.com/GTHMG11363
更多推荐
发布评论