两个查询之间的组合

时间:2016-07-25 15:46:14

标签: mysql sql

我有两个大问题,他们分别工作和生产我想要的东西。

查询1:

SELECT pt.map, pt.time, pt.jumps, pt.id
FROM playertimes pt
WHERE auth = '[U:1:204506329]' AND style = 0 ORDER BY map;

(在行动中,[U:1:204506329]是一个变量)

查询2:

SELECT COUNT(*) rank
FROM playertimes pt
LEFT JOIN users u ON u.auth = pt.auth
WHERE pt.time <= (
    SELECT time
    FROM playertimes
    WHERE map = 'bhop_horsepoop_8' AND style = 0 AND auth = '[U:1:204506329]'
    LIMIT 1
) AND pt.map = 'bhop_horsepoop_8' AND pt.style = 0
ORDER BY time ASC LIMIT 1;

(再次,[U:1:204506329]和bhop_horsepoop_8是变量)

查询1的目的是检索maptimejumpid s的列表,其中auth是我的内容定义

查询2的目的是在定义的auth的{​​{1}}表中查找playertimes的位置(&#39;排名&#39;), map。我的意图是将这两者加到一个查询中。在第一个查询中接收我想要的内容,然后为查询1中检索到的每一行获取style字段。

这就是我的尝试:

rank

结果最终非常不正确!

以下是此查询的结果集(phpMyAdmin):

Query resultset

如您所见,SELECT pt.map, pt.time, pt.jumps, pt.id, ( SELECT COUNT(*) AS rank FROM playertimes pt LEFT JOIN users u ON u.auth = pt.auth WHERE pt.time <= ( SELECT time FROM playertimes WHERE map = pt.map AND style = 0 AND auth = '[U:1:204506329]' LIMIT 1 ) AND pt.map = map AND pt.style = 0 ORDER BY time ASC LIMIT 1 ) rank FROM playertimes pt WHERE auth = '[U:1:204506329]' AND style = 0 ORDER BY map; 始终相同,应该更改每一行。虽然我想要的是rank根据同一行的rank进行计算。

我真的被困住了。非常感谢帮助(以及解释,如果可能的话)!

更新:我已经尝试了Mike的解决方案,这是我最终得到的查询:

map

以下是MariaDB返回的错误:

SELECT * FROM (
  SELECT pt.map, pt.time, pt.jumps, pt.id, pt.auth
    FROM playertimes pt
        WHERE auth = '[U:1:204506329]' AND style = 0
            ORDER BY map
        ) a
    LEFT JOIN
    (
        SELECT COUNT(*) rank
            FROM playertimes pt
                LEFT JOIN users u
                    ON u.auth = pt.auth
                        WHERE pt.time <=(
                            SELECT time, auth
                                FROM playertimes
                                    WHERE map = a.map AND style = 0 AND auth = '[U:1:204506329]'
                                        LIMIT 1
  )
        AND pt.map = a.map AND pt.style = 0
            ORDER BY time ASC
                LIMIT 1
                    ) b
    ON a.auth = b.auth;

我不确定如何解决这个问题。但是,我尝试使用常量字符串填充 #1054 - Unknown column 'a.map' in 'where clause' ,这是输出:

WHERE a.map

这当然是因为我试图为操作数选择 #1241 - Operand should contain 1 column(s) time(而我只能选择一个字段),否则我必须这样做{{ 1}}将在ON子句中抛出一个关于auth是未知列的错误。

更新2:我已经找到了部分解决方案,但查询有效,但ON a.auth = b.auth仅适用于我定义的b.auth

rank

我无法想出一个能为当前行计算map的解决方案。

0 个答案:

没有答案