gpt4 book ai didi

PHP MySQL 高分表

转载 作者:可可西里 更新时间:2023-11-01 07:05:17 25 4
gpt4 key购买 nike

我加入了 3 个表以显示用户得分最高的表

我的表格

游戏日志:

 ---ID---user_ID---score---time---
| 1 52 567 10 |
| 2 53 641 13 |
| 3 52 465 8 |
| 4 53 451 14 |
---------------------------------

用户:

 ---ID---name---countyid---
| 52 Dave 1 |
| 53 John 2 |
------------------------

县:

  ---countyid---countyname---
| 1 Orange wichit |
| 2 Orange clemts |
--------------------------

SQL:

SELECT * FROM game_log 
INNER JOIN users ON game_log.user_ID=users.ID
INNER JOIN county ON users.countyid=county.countyid
ORDER BY game_log.score DESC , game_log.time LIMIT 20";

上面的代码给了我这个结果:

Rank---Name--------County------Score---Time
1 John Orange clemts 641 13
2 Dave Orange wichit 567 10
3 John Orange clemts 465 8
4 Dave Orange wichit 451 14

我的问题是我希望高分表显示得分最高的前 20 个用户,而不是 20 个最高分。

像这样:

Rank---Name--------County------Score---Time
1 John Orange clemts 641 13
2 Dave Orange wichit 567 10

需要一些帮助,不熟悉连接表;-)

最佳答案

此方法将显示前 20 位用户和每个用户的最高分数,如果他们有多个相同分数的实例,它将显示最早的信息(该用户和分数的最低时间值)。

SELECT *
FROM game_log gl
INNER JOIN users u
ON gl.user_ID = u.ID
INNER JOIN county c
ON u.countyid = c.countyid
WHERE not exists (select 1
from game_log gl2
where gl2.user_id = gl.user_id
and gl2.score > gl.score)
and not exists (select 1
from game_log gl2
where gl2.user_id = gl.user_id
and gl2.time < gl.time
and gl2.score = gl.score)
ORDER BY gl.score DESC, gl.time LIMIT 20;

如果不这样做,如果前 20 名中的同一用户有 2 次以上相同的分数,他们将被列出 2 次以上,并且您不会通过使用 LIMIT 20 找回 20 人,因为同一个人会从那 20 行中向上 N 行。

SQL Fiddle 此处显示的数据有领带:http://sqlfiddle.com/#!2/0ac931/5/0

关于PHP MySQL 高分表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21976665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com