0

我有以下查询:

SELECT
    A.player, A.score, B.kills, C.deaths, D.killed AS most_killed, D.kills AS most_killed_count, E.player AS most_killed_by, E.kills AS most_killed_by_count
FROM
(SELECT player, score FROM pvpr_scores WHERE player = 'Neutronix' AND milestone = 'default') AS A
LEFT JOIN (SELECT player, COUNT(*) AS kills FROM pvpr_kills WHERE player = 'Neutronix' AND milestone = 'default') AS B ON B.player= A.player
LEFT JOIN (SELECT killed, COUNT(*) AS deaths FROM pvpr_kills WHERE killed = 'Neutronix' AND milestone = 'default') AS C ON C.killed= A.player
LEFT JOIN (SELECT player, killed, COUNT(*) AS kills FROM pvpr_kills WHERE player = 'Neutronix' GROUP BY killed ORDER BY kills DESC LIMIT 1) AS D ON D.player= A.player
LEFT JOIN (SELECT player, killed, COUNT(*) AS kills FROM pvpr_kills WHERE killed = 'Neutronix' GROUP BY player ORDER BY kills DESC LIMIT 1) AS E ON E.killed= A.player

但是,如果这些子查询之一返回 null,则整个查询将失败。我想让空查询改为在返回的结果集中(1 行)使这些列为空。

如果我更改Neutronix为不在数据库中的内容,例如dfdsjf,则会出现错误。

编辑:这是我所做的修复。

SELECT
    A.player, A.score, B.kills, C.deaths, D.killed AS most_killed, D.kills AS most_killed_count, E.player AS most_killed_by, E.kills AS most_killed_by_count
FROM
(SELECT player, score FROM pvpr_scores WHERE player = 'Gutterknife' AND milestone = 'default') AS A
LEFT OUTER JOIN (SELECT COUNT(*) AS kills FROM pvpr_kills WHERE player = 'Gutterknife' AND milestone = 'default') AS B ON TRUE
LEFT OUTER JOIN (SELECT COUNT(*) AS deaths FROM pvpr_kills WHERE killed = 'Gutterknife' AND milestone = 'default') AS C ON TRUE
LEFT OUTER JOIN (SELECT killed, COUNT(*) AS kills FROM pvpr_kills WHERE player = 'Gutterknife' GROUP BY killed ORDER BY kills DESC LIMIT 1) AS D ON TRUE
LEFT OUTER JOIN (SELECT player, COUNT(*) AS kills FROM pvpr_kills WHERE killed = 'Gutterknife' GROUP BY player ORDER BY kills DESC LIMIT 1) AS E ON TRUE
4

2 回答 2

1

我认为问题不在于子查询。我认为问题是驱动程序查询:

FROM (SELECT player, score
      FROM pvpr_scores
      WHERE player = 'Neutronix' AND milestone = 'default'
     ) AS A . . . 

如果没有与此查询匹配,则在其他查询中没有可匹配的行。

我不太确定如何解决这个问题。我想你可以这样做:

FROM (select 'Neutronix' as player) t left outer join
     (SELECT player, score
      FROM pvpr_scores
      WHERE player = 'Neutronix' AND milestone = 'default'
     ) AS A
     on t.player = a.player left outer join . . . 

然后将子句join其余部分中的条件更改为,而不是.fromt.a.

于 2013-07-20T02:10:28.493 回答
0

为什么要在 FROM 上运行子选择 - 你可以直接 FROM 分数。

您还可以在“ON”子句上有多个条件 - 例如 ON b.player = a.player 和 b.milestone = a.milestone。

而不是子选择,您可以使用 SUM 和 IF 组合来为您提供结果 - 例如 SUM(IF(row not null, 1, 0)) - 这基本上是 1 和 0 的总和。

显然,这没有经过测试,但我会按照以下方式处理您的查询:

SELECT scores.player,
        scores.score,
        sum(if(kills.player is not null, 1, 0) as kills,
        sum(if(deaths.player is not null, 1, 0) as deaths
        killed.kills as most_killed,
        killed.killed as most_killed_count,
        killed_by.kills as most_killed_by_count,
        killed_by.killed as most_killed_by
FROM pvpr_scores as scores
LEFT JOIN pvpr_kills as kills
    on kills.player = player
    and kills.milestone = scores.milestone
LEFT JOIN pvpr_deaths as deaths
    on deaths.player = player
    and deaths.milestone = scores.milestone
LEFT JOIN (SELECT killed, player, count(killed) as kills
            FROM kills
            GROUP BY killed, player
            HAVING MAX( count(killed) ) ) as killed
    ON killed.player = scores.player
LEFT JOIN (SELECT killed, player, count(player) as kills
            FROM kills
            GROUP BY player, killed
            HAVING MAX( count(player) ) ) as killed_by
    ON killed_by.killed = scores.player

WHERE scores.player = 'neutronix'
  AND scores.milestone = 'default'

   GROUP BY scores.player
于 2013-07-20T02:45:56.977 回答