MySQL LEFT JOIN Query Shows Same Value
I am trying to make it so that this query below counts rows from another table that have the username, then deaths that have killer. There is 1 row per username in the stats table,
Solution 1:
SELECT st.*,
SUM(pvp.killer = st.username) AS kills,
SUM(pvp.username = st.username) as deaths
FROM stats AS st
LEFT JOIN pvp ON pvp.username = st.username OR pvp.killer = st.username
WHERE st.username = "Username"
To do it for everyone, use GROUP BY
:
SELECT st.*,
SUM(pvp.killer = st.username) AS kills,
SUM(pvp.username = st.username) as deaths
FROM stats AS st
LEFT JOIN pvp ON pvp.username = st.username OR pvp.killer = st.username
GROUP BY st.username
Solution 2:
COUNT(foo)
and COUNT(bar)
both count the number of rows in the resultset, you might as well say COUNT(*)
in both cases.
You need to COUNT(*) FROM ... GROUP BY killer
to get the number of killers, for example. Not sure what the table structure is to give you a full answer.
Post a Comment for "MySQL LEFT JOIN Query Shows Same Value"