我有很多球员记录,一堆世界纪录,还有一些被禁止的球员:
describe records
+--------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| type | varchar(3) | NO | MUL | NULL | |
| map | varchar(31) | NO | MUL | NULL | |
| authid | varchar(31) | NO | | NULL | |
| time | decimal(13,6) | NO | | NULL | |
| date | datetime | NO | | NULL | |
+--------+---------------+------+-----+---------+----------------+
describe wrs
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| map | varchar(31) | NO | MUL | NULL | |
| route | varchar(31) | NO | | NULL | |
| time | decimal(5,2) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
describe banned
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| authid | varchar(31) | NO | UNI | NULL | |
+--------+-------------+------+-----+---------+----------------+
我正在尝试从time
表格中为每个唯一records
选择最小map
,其中authid
(玩家标识符)未在{{1}中找到表格。
我还希望从banned
检索相同time
的世界记录wrs
。
我希望仅在map
等于"亲"时才从map
选择authid
,time
和records
但是如果type
不等于"亲"我希望保留map
来自time
中相应行的唯一地图和wrs
列表。
我的问题是在检索最低type
/ authid
时从records
表中检索正确/对应的time
。我设法做了这样的事情:
map
但它没有考虑被禁止的玩家,因为在选择最小SELECT m.map, minTime, wrTime, wrTime / minTime AS ratio, minTime - wrTime AS diffTime FROM
# get all unique maps from records table
(SELECT map FROM records GROUP BY map) AS m
LEFT JOIN
# find min time for each map where type is pro
(SELECT type, map, MIN(time) AS minTime FROM records WHERE type = "pro" GROUP BY map) AS p
ON m.map = p.map
LEFT JOIN
# find world record time for each map
(SELECT map, MIN(time) AS wrTime FROM wrs GROUP BY map) AS w
ON m.map = w.map
时我无法检索到正确的authid
值(除此之外:我甚至不确定会使用正确的time
在where子句中)。如果我有正确的type
,我可以轻松地在查询中添加类似以下的内容来忽略被禁玩家:
authid
希望我有道理。如果没有,请询问更多信息。
答案 0 :(得分:1)
我不确定我是否完全理解了您的问题,但我认为以下SQL可能会对您有所帮助:
我的问题是在检索最小时间/地图时从记录表中检索正确/相应的authid。 “
SELECT
map,
MIN(time) AS minTime
FROM records
WHERE
authid NOT IN (SELECT authId FROM banned)
GROUP BY map
答案 1 :(得分:0)
试试这个,希望我没有误解你的意图。
SELECT
m.map,
m.minTime,
w.wrTime,
w.wrTime / m.minTime AS ratio,
m.minTime - w.wrTime AS diffTime
FROM
records r
INNER JOIN
-- get all unique maps from records table
(SELECT map, MIN(time) AS minTime FROM records WHERE type = "pro" GROUP BY map) m ON r.map = m.map AND r.time = m.minTime
LEFT JOIN
-- find world record time for each map
(SELECT map, MIN(time) AS wrTime FROM wrs GROUP BY map) w ON m.map = w.map
LEFT JOIN banned b ON r.authid = b.authid
WHERE b.authid IS NULL
答案 2 :(得分:0)
对于那些感兴趣的人,这就是我最终解决问题的方法(在Piyg的答案的帮助下):
SELECT
m.map,
p.minTime,
w.wrTime,
w.wrTime / p.minTime AS ratio,
p.minTime - w.wrTime AS diffTime
FROM
(SELECT
map
FROM records
GROUP BY map) m
LEFT JOIN
(SELECT
map,
MIN(time) AS minTime
FROM records
WHERE
authid NOT IN (SELECT authid FROM banned)
AND
type = "pro"
GROUP BY map) p
ON m.map = p.map
LEFT JOIN
(SELECT
map,
MIN(time) AS wrTime
FROM wrs
GROUP BY map) w
ON m.map = w.map