gpt4 book ai didi

mysql - 如何连接指向单个主键的多个外键?

转载 作者:行者123 更新时间:2023-12-01 00:08:41 25 4
gpt4 key购买 nike

亲爱的,

我在下面有两个单独的 sql 语句。他们独立工作正常。

select teams.team_name_eng AS "TNAME1"
from matches, teams
where matches.team1_uid = teams.tid;

select teams.team_name_eng AS "TNAME2"
from matches, teams
where matches.team2_uid = teams.tid

如何将这些语句合并为一个语句?我希望输出是

TNAM1     TNAME2
----- ------
playerA PlayerB
playerC playerD

注意:外键关系已经创建。

谢谢,

最佳答案

使用 Join 获取答案

select teams1.team_name_eng AS "TNAME1",teams2.team_name_eng AS "TNAME2"
from matches
join teams as teams1 on teams1.tid = matches.team1_uid
join teams as teams2 on teams2.tid = matches.team2_uid

关于mysql - 如何连接指向单个主键的多个外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52551686/

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