gpt4 book ai didi

postgresql - Postgres 从表行中选择

转载 作者:行者123 更新时间:2023-11-29 13:00:12 24 4
gpt4 key购买 nike

我有一个表团队,其中包含像这样的列

team_id | name    | competition_id
--------+---------+------
1 | name 1 | 10
2 | name 2 | 10

我还有这张 table 固定装置

fixture_id  | home_team_id | away_team_id | competition_id
--------+--------------+--------------+--------------
1 | 1 | 2 | 10
2 | 2 | 1 | 10

表 fixtures 的主键是 fixture_id, home_team_id, away_team_id,和 competition_id。 home_team、away_team 和 competition 也是外键。

如何从赛程中选择 home_team_name 、 away_team_name 。

最佳答案

您可以两次加入 teams 表中的 fixtures 表 - 一次用于主队,一次用于客队:

SELECT  fixture_id, home.name, away.name
FROM fixtures f
JOIN teams home on home.team_id = f.home_team_id
JOIN teams away on away.team_id = f.way_team_id

关于postgresql - Postgres 从表行中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32745492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com