我正在尝试这样做,以便下面的查询计算来自另一个具有用户名的表中的行。统计信息表中每个用户名有1行,但pvp表中有多行用户名。从pvptable加入的所有列都显示为null,并且kills&使用COUNT动态生成的死亡数。这是SQL查询。
SELECT
*,
pvptable.username AS USER,
COUNT(pvptable.killer) AS kills,
COUNT(pvptable.username) AS deaths,
ROUND(
COUNT(pvptable.killer) / COUNT(pvptable.username),
2
) AS kd
FROM
stats AS st
LEFT JOIN pvp AS pvptable
ON pvptable.killer = "Username"
AND pvptable.username = "Username"
WHERE st.username = "Username" ;
答案 0 :(得分:0)
您正在混合where
和join
条件:
join
where
因此,要使您的查询有效:
SELECT
*, pvptable.username as user,
COUNT(pvptable.killer) as kills,
COUNT(pvptable.username) as deaths,
ROUND(COUNT(pvptable.killer) / COUNT(pvptable.username), 2) AS kd
FROM
stats as st
LEFT JOIN pvp as pvptable
ON pvptable.killer=st.Username -- Add some other relations if you need them
WHERE
st.username="Username";
答案 1 :(得分:0)
为你想要的每个属性做一个连接,然后使用COUNT(DISTINCT id)(假设它是pvp表的唯一键),如下所示: -
SELECT st.username as user,
COUNT(DISTINCT pvptable_kills.id) as kills,
COUNT(DISTINCT pvptable_deaths.id) as deaths,
ROUND(COUNT(DISTINCT pvptable_kills.id) / COUNT(DISTINCT pvptable_deaths.id), 2) AS kd
FROM stats as st
LEFT OUTER JOIN pvp as pvptable_kills ON pvptable_kills.killer=st.Username
LEFT OUTER JOIN pvp as pvptable_deaths ON pvptable_deaths.username=st.Username
WHERE st.username="Username"
GROUP BY st.username