doufan6544 2011-06-06 15:26
浏览 84
已采纳

引用多个外键php mysql

I'm very new to php/MySQL and I'm having a bit of trouble. Help would be much appreciated.

I have 2 tables laid out as such:

table team

team_id,team_name

table schedule

game_id,game_time,team1_id,team2_id,location

schedule.team1_id and schedule.team2_id are both foreign keys to team.team_id.

I'm trying to reference team_name using team1_id and team2_id but I can only ever seem to get the name for team1. This is the query I've used unsuccessfully.

SELECT * FROM team
AS t JOIN schedule AS s 
ON t.team_id = s.team1_id 
WHERE location='1';

My attempt to output the data:

while (mysql_fetch_assoc($result)) {
    echo $row['team_name'];
}

It's quite obvious to me why this isn't working, as the tables are joined on only one of the columns I need. Help! I'm completely lost as to how to solve this problem. I believe there is a simple solution, but I can't seem to find it!

  • 写回答

1条回答 默认 最新

  • dqwh2717 2011-06-06 15:29
    关注

    It's not quite clear on what you would like the results of your query to look like, but I am assuming you would like to get information about both teams involved in each game. To do so, you'll have to join the team table twice:

    SELECT t1.name, t2.name, s.* 
    FROM schedule AS s 
    INNER JOIN team AS t1 ON t1.team_id = s.team1_id 
    INNER JOIN team AS t2 ON t2.team_id = s.team2_id 
    WHERE s.location='1';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line