我有以下查询:
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,则整个查询将失败。我想让null查询在返回的结果集(1行)中使这些列为null。
如果我将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
答案 0 :(得分: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
子句的其余部分中的from
条件更改为t.
而不是a.
。
答案 1 :(得分:0)
为什么你在FROM上运行子选择 - 你可能只是直接的FROM分数。
你也可以对'ON'条款有多个条件 - 例如ON b.player = a.player和b.milestone = a.milestone。
您可以使用SUM和IF组合来提供结果,而不是子选择 - 例如SUM(IF(行不为空,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