我有3张桌子:
我在这里花了几个小时来构建这个查询:
SELECT * FROM `soccer_players`
INNER JOIN `playersPoints` ON `soccer_players`.player_id
=`playersPoints`.playerID
INNER JOIN `soccer_fixtures`
ON soccer_players.team_id = soccer_fixtures.home_team_id
WHERE soccer_fixtures.home_team_id IN (528,529,530, 531, 532, 533,
534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545)
AND soccer_fixtures.season_id = 2201
AND position != 'Coach' AND soccer_fixtures.status = "NS"
AND soccer_fixtures.game_week = 32
ORDER BY `soccer_players`.`player_name` ASC
我的结果很好,但我的问题是我想在
之后添加INNER JOIN `soccer_fixtures`
ON soccer_players.team_id = soccer_fixtures.home_team_id
这样:
INNER JOIN `soccer_fixtures`
ON soccer_players.team_id = soccer_fixtures.away_team_id
我的目标是从桌子上过滤足球运动员,将他们与这些球员的分数联系起来,然后将这个结果加入到我可以显示的这个游戏周的装置中,哪个玩家正在哪个日期玩。
我知道问题是什么,但我不知道如何解决它。
不能有2个INNER JOIN
具有相同的名称,但如何将这两个INNER JOIN
分组为一个?
修改
我也尝试了以下内容:
SELECT * FROM `soccer_players`
INNER JOIN `playersPoints` ON `soccer_players`.player_id
=`playersPoints`.playerID
INNER JOIN `soccer_fixtures`
RIGHT JOIN soccer_players
(
ON soccer_fixtures.team_id = soccer_fixtures.home_team_id
ON soccer_fixtures.team_id = soccer_fixtures.a_team_id
)
WHERE
soccer_fixtures.home_team_id IN (528,529,530, 531, 532, 533,
534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545)
OR
soccer_fixtures.a_team_id IN (528,529,530, 531, 532, 533,
534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545)
AND soccer_fixtures.season_id = 2201
AND position != 'Coach' AND soccer_fixtures.status = "NS"
AND soccer_fixtures.game_week = 32
ORDER BY `soccer_players`.`player_name` ASC
没有成功,但我认为这与解决方案很接近。
EDIT2:
好的,我现在有这个解决方案: 结果很精彩!我对这种性能感到疑惑,现在花了很长时间才得到正确的结果。我可以将查询修改为lil,以便更快地从我的3个tabkles获得结果吗?
现在是我的修改版本:
Thorsten的大事!
答案 0 :(得分:1)
您希望在第32周选择与状态NS匹配的玩家。因此,请从玩家中进行选择。条件(与状态和周匹配)属于WHERE
子句。由于玩家可以拥有多个积分,因此将每个玩家点和外部加起来加入玩家。
select sp.name, coalesce(pp.total_points, 0) as points
from soccer_players sp
left join
(
select playerid, sum(points) as total_points
from playerspoints
group by playerid
) pp on pp.playerid = sp.player_id
where exists
(
select *
from soccer_fixtures sf
where sf.season_id = 2201
and sf.game_week = 32
and sf.status = 'NS'
and sp.team_id in (sf.home_team_id, sf.away_team_id)
);
(不是将点子查询放在FROM
子句中,您也可以在SELECT
子句中使用它,但通常我们更喜欢FROM
中的外连接,所以我们可以如有必要,请选择多个列。)