为简单起见,我会在这里将我的问题描述为理论问题.
想象一下,你有两个表 – MATCHES和FIGHTERS. ‘战士’有一个战士列表(pk_fighter_id,战斗机名称),其中任何两个都可以安排互相攻击. ‘匹配’可能是一个三字段表(pk_fight_num,fk_fighter_id1,fk_fighter_id2),它跟踪这些配对. Fighter1和Fighter2是引用Fighters表中条目的外键.
我需要获得所有战斗的清单,以显示谁在战斗谁,即“23123 | Pacquaio | Marquez.”我如何构建我的查询呢?
我想象的是:
select fk_fighter_id1,fk_fighter_id2
from matches
inner join fighters on matches.fk_fighter_id1=fighters.pk_fighter_id
inner join fighters on matches.fk_fighter_id2=fighters.pk_fighter_id;
当我尝试在Access中嘲笑它时,我将它拼凑在一起,但它不起作用:
SELECT matches.match_no,fighters.fighter_name,fighters.fighter_name
FROM fighters
INNER JOIN matches ON (fighters.fighter_id = matches.fighter2) AND (fighters.fighter_id = matches.fighter1);
那么,有什么想法吗?我只是不知道从哪里开始.
最佳答案
你接近你想要的.您只需要为表以及具有相同名称的投影列定义唯一别名.
原文链接:https://www.f2er.com/mysql/433784.htmlselect a.pk_fight_num,b.fighter_name firstFighter,-- <<== needed also
c.fighter_name secondFighter -- <<== needed also
from matches a
inner join fighters b
on a.fk_fighter_id1 = b.pk_fighter_id
inner join fighters c
on a.fk_fighter_id2 = c.pk_fighter_id;