我正在尝试使它下面的该查询计数具有用户名的另一个表中的行。 stats表中的每个用户名都有1行,而pvp表中的每个用户名都包含多行。从pvptable连接的所有列均显示为null,并且使用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" ;
最佳答案
您正在混合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";