Mysql 左外连接过滤依据
Mysql Left outer join filter by
我正在尝试从 MySQL 服务器中的数据中提取排行榜。就是显示某些玩家在每张地图上的单圈时间。
我目前提出的查询是这样的:
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;
这个 return 是正确的结果,但是我需要通过地图过滤它。示例 table 可在 http://sqlfiddle.com/#!2/3e9c6/1
中找到
此查询将响应:
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
这几乎是正确的,但是我的查询只需要 return 选择的地图而不是所有地图的时间。正确的查询应该响应所选地图的前 15 次,例如 "surf_utop" 应该响应以下 table:
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
我已经查看了其他问题,例如 SQL Select only rows with Max Value on a Column 但是没能弄明白。
所以只需将所选地图添加到您的 WHERE 中即可。
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;
结果:
+-------------------+-----------------+-----------+-------+------------+
| 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 |
+-------------------+-----------------+-----------+-------+------------+
你不需要再JOIN
整个table,你可以使用:
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;
我正在尝试从 MySQL 服务器中的数据中提取排行榜。就是显示某些玩家在每张地图上的单圈时间。
我目前提出的查询是这样的:
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;
这个 return 是正确的结果,但是我需要通过地图过滤它。示例 table 可在 http://sqlfiddle.com/#!2/3e9c6/1
中找到此查询将响应:
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
这几乎是正确的,但是我的查询只需要 return 选择的地图而不是所有地图的时间。正确的查询应该响应所选地图的前 15 次,例如 "surf_utop" 应该响应以下 table:
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
我已经查看了其他问题,例如 SQL Select only rows with Max Value on a Column 但是没能弄明白。
所以只需将所选地图添加到您的 WHERE 中即可。
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;
结果:
+-------------------+-----------------+-----------+-------+------------+
| 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 |
+-------------------+-----------------+-----------+-------+------------+
你不需要再JOIN
整个table,你可以使用:
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;