简体   繁体   English

Mysql左外部联接过滤器

[英]Mysql Left outer join filter by

I'm trying to extract a leaderboard from data in a MySQL server. 我正在尝试从MySQL服务器中的数据提取排行榜。 It's to show lap times on each map by certain players. 这是为了显示某些玩家在每张地图上的圈速。

My current query I came up with so far is this: 到目前为止,我目前提出的查询是:

select d1.*
from surf_times d1
left outer join surf_times d2
on (d1.Name = d2.Name and d1.Time > d2.Time)
where d2.Name is null
order by Time;

This returns the correct results however I need to filter it by map. 这将返回正确的结果,但是我需要按地图进行过滤。 An example table can be found at http://sqlfiddle.com/#!2/3e9c6/1 可以在http://sqlfiddle.com/#!2/3e9c6/1上找到示例表。

This query will respond with: 该查询将响应:

SteamID             Name                    Map             Time    Date
76561197991519598   Kuratheris              surf_utop       60.05   1445107360
76561198129490626   xXNightw0lfXx           surf_ace        60.84   1445106920
76561198156238243   ☆ The Pic ☆         surf_utop       62.35   1445107724
76561198049179442   J4N412N3                surf_utop       69.53   1445107519
76561197994977992   Rockape2620             surf_ace        72.26   1445107047

This is almost correct, however my query needs to only return the map selected rather than times from all maps. 这几乎是正确的,但是我的查询只需要返回选择的地图,而不是所有地图的时间。 The correct query should respond with the top 15 times for the selected map for example "surf_utop" should respond with the following table: 正确的查询应以所选地图的前15次响应,例如“ surf_utop”应以下表响应:

SteamID             Name                    Map             Time    Date
76561197991519598   Kuratheris              surf_utop       60.05   1445107360
76561198156238243   ☆ The Pic ☆         surf_utop       62.35   1445107724
76561198049179442   J4N412N3                surf_utop       69.53   1445107519

I've had a look at other questions such as SQL Select only rows with Max Value on a Column however have not been able to figure it out. 我看过其他问题,例如SQL仅选择在列上具有最大值的行,但是无法弄清楚。

so just add on to your WHERE the selected map. 因此,只需将所选地图添加到您的位置即可。

select d1.*
from surf_times d1
left outer join surf_times d2
on (d1.Name = d2.Name and d1.Time > d2.Time)
where d2.Name is null AND d1.map = 'surf_utop'
order by Time
limit 15;

fiddle example 小提琴的例子

result: 结果:

+-------------------+-----------------+-----------+-------+------------+
|      SteamID      |      Name       |    Map    | Time  |    Date    |
+-------------------+-----------------+-----------+-------+------------+
| 76561197991519598 | Kuratheris      | surf_utop | 60.05 | 1445107360 |
| 76561198156238243 | ☆ The Pic ☆ | surf_utop | 62.35 | 1445107724 |
| 76561198049179442 | J4N412N3        | surf_utop | 69.53 | 1445107519 |
+-------------------+-----------------+-----------+-------+------------+

You don't need to JOIN the whole table again, you can use: 您无需再次JOIN整个表,可以使用:

SELECT st.*
FROM surf_times st
WHERE st.Time = 
    (SELECT MIN(t.Time) 
    FROM surf_times t 
    WHERE t.SteamID = st.SteamID AND t.Map = st.Map) 
        AND st.Map = 'surf_utop' -- or any other map
GROUP BY st.SteamID
ORDER BY st.Time
LIMIT 15;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM