我正在使用 MYSQL,但在获得我想要的预期结果时遇到了一些麻烦。
我现在正在处理 4 张桌子......
matchopponents
--------------
opp1id
opp2id
matchid
matches
-------------
id
title
players
------------
id
playeralias
ratings
------------
ratingid
ratingvalue
matchid
我需要一个查询,它将从 matchopponents 表中选择每一行,根据 opp1id 和 opp2id 区分 playeralias,从 matches 表中获取比赛标题,然后对 matchopponent 表中每个受尊重的比赛的平均总评分进行四舍五入。到目前为止,我的查询已接近预期结果,但它仅从 matchopponents 表中返回一行。目前我在 matchopponents 表中有 2 行:
opp1id opp2id matchid
2 3 11 4 5 12
以下查询返回
Matchtitle Player1 Player2 Rating MarineKing vs Ryung MarineKing Ryung 3
这对第一行来说很棒......(opp1id 2,opp2id 3,matchid 11)
但是第二行没有返回任何内容......当我删除 LEFT JOIN 评级时,两者都被显示。我已确保所有正确的 ID 均已到位且正确无误。:(
任何有关为什么会发生这种情况的帮助将不胜感激:)
SELECT
m.title AS "Match Title",
p1.playeralias AS "Player 1",
p2.playeralias AS "Player 2",
ROUND(IFNULL(AVG(r.ratingvalue), 1)) AS "Rating"
FROM
matchopponents AS MO
JOIN matches AS m ON mo.matchid = m.id
JOIN players AS p1 ON mo.opp1id = p1.id
JOIN players AS p2 ON mo.opp2id = p2.id
LEFT JOIN ratings r
ON
r.matchid = mo.matchid