Mysql左外连接过滤器

时间:2015-10-17 21:47:19

标签: mysql left-join

我正在尝试从MySQL服务器中的数据中提取排​​行榜。它是由某些玩家在每张地图上显示单圈时间。

我目前提出的当前查询是:

<local:CardControl  VisualState="{Binding State.Value}" />

这会返回正确的结果,但我需要按地图过滤它。可以在http://sqlfiddle.com/#!2/3e9c6/1

找到示例表

此查询将回复:

private void set_4_CardControl_VisualState(object instance, object Value)
{
    var that = (global::MeetEric.UI.Controls.CardControl)instance;
    that.VisualState = (global::MeetEric.ViewModels.CardStates)Value;
}

这几乎是正确的,但是我的查询只需返回所选地图而不是所有地图的时间。正确的查询应该响应所选地图的前15次,例如“surf_utop”应该响应下表:

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;

我已经查看了其他问题,例如SQL Select only rows with Max Value on a Column,但无法弄明白。

2 个答案:

答案 0 :(得分:1)

所以只需在你的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;

fiddle example

结果:

+-------------------+-----------------+-----------+-------+------------+
|      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 |
+-------------------+-----------------+-----------+-------+------------+

答案 1 :(得分:1)

您不需要再次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;