我在哪里将where子句放在此查询中以过滤结果?

时间:2015-01-02 06:40:44

标签: mysql sql select join where

我正在尝试在查询中插入where子句,只选择某些行。

这是查询:

SELECT m.MapName, SEC_TO_TIME(TRUNCATE(t.Time,3)) AS Time, p.User
FROM times t INNER JOIN
     maps m INNER JOIN
     players p
     ON t.MapID = m.MapID INNER JOIN
     (SELECT t.MapId, MIN(t.time) as time
      FROM times t
      GROUP BY t.MapId
     ) tmin
     ON tmin.MapId = t.MapId and tmin.time = t.time and p.PlayerID = t.PlayerID;

我想要做的是添加一个WHERE子句,只选择表styletype = 0和times = 0的选择时间。

当前运行该查询的所有时间都以以下形式返回:

MapName:       Time:            PlayerID:
map_name       00:00:09.440     player_name1
map_name2      xx:xx:xx:xxx     player_name2
map_name3      xx:xx:xx:xxx     player_name3

如果这有帮助,表times的布局如下:

MapID:      Type:      Style:      Time:
1           0          0           214.235
2           1          0           12.325
3           0          1           321.333

我想要做的是上面的相同查询,但只选择typestyle为0的时间。

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT m.MapName, SEC_TO_TIME(TRUNCATE(t.Time,3)) AS Time, p.User
FROM times t 
INNER JOIN maps m ON t.MapID = m.MapID 
INNER JOIN players p ON p.PlayerID = t.PlayerID
INNER JOIN (SELECT t.MapId, MIN(t.time) as time
            FROM times t
            WHERE t.style = 0 and t.type = 0 
            GROUP BY t.MapId
          ) tmin ON tmin.MapId = t.MapId and tmin.time = t.time;

答案 1 :(得分:0)

试试这个:

...           ON     tmin.MapId = t.MapId
                 and tmin.time = t.time
                 and p.PlayerID = t.PlayerID
 WHERE times.type =0 and times.style = 0;