一张表有 2 个字段引用同一张表
我有一个表(GAMES),总共包含 11 个字段(包括 ID、主键)。 该表的 2 个字段(通过外键)引用同一个外部表(TEAMS 表),这些是 HOME_TEAM 和 AWAY_TEAM 字段
TEAMS 表包含所有球队,包括他们的名称、地址、徽标等。
我的问题是如下:
当我在 GAMES 表中引用同一个表的 2 个字段时,如何正确显示这些字段。当我的意思是显示时,我的意思是我想显示团队的名称(存储在表 TEAMS 中),而不是存储在表 GAMES 中的外键。 我可以正确显示外键,但是如何显示团队的名称呢?
非常感谢任何帮助,谢谢。
这就是我现在所拥有的:
mysql_select_db($database_check_mag, $check_mag);
$query_getPosts = "SELECT games.id_game, games.seizoen, games.date, games.type, games.hometeam, games.awayteam, teams.id_team, teams.naam_team, teams.logo, teams.adres FROM games INNER JOIN teams ORDER BY games.date DESC";
$query_limit_getPosts = sprintf("%s LIMIT %d, %d", $query_getPosts, $startRow_getPosts, $maxRows_getPosts);
$getPosts = mysql_query($query_limit_getPosts, $check_mag) or die(mysql_error());
$row_getPosts = mysql_fetch_assoc($getPosts);
这就是我显示它的方式:
<table width="100%">
<tr>
<th scope="col" align="left">Date</th>
<th scope="col" align="left">Type</th>
<th scope="col" align="left">Game</th>
<th scope="col"> </th>
<th scope="col"> </th>
</tr>
<?php do { ?>
<tr>
<td align="left"><?php echo $row_getPosts['date']; ?></td>
<td align="left"><?php echo $row_getPosts['type']; ?></td>
<td align="left"><?php echo $row_getPosts['hometeam']; ?> - <?php echo $row_getPosts['awayteam']; ?></td>
<td><a href="games_edit.php?id_game=<?php echo $row_getPosts['id_game']; ?>">EDIT</a></td>
<td><a href="games_delete.php?id_game=<?php echo $row_getPosts['id_game']; ?>">DELETE</a></td>
</tr>
<?php } while ($row_getPosts = mysql_fetch_assoc($getPosts)); ?>
</table>
所以现在 echo $row_getPosts['hometeam'] 和 echo $row_getPosts['awayteam'] 正确显示 id_team 字段,但我希望它们显示 naam_team 字段,当然是不同的......
I have a table (GAMES) containing 11 fields total (including the ID, primary key).
2 fields of this table reference (via foreign keys) to the same external table (TEAMS table), these are the HOME_TEAM and AWAY_TEAM fields
The table TEAMS contains all the teams, including their name, address, logo, etc.
My problem is as follows:
As i reference to the same table for 2 fields in the GAMES table how can i display these correctly. When I mean display i mean that i want to display the name of the team (stowed in the table TEAMS), and not the foreign key which is stowed in the table GAMES.
I get to display the foerign keys correctly, but how can i display the name of the team instead?
Any help much appreciated, thanks.
This is what i have now:
mysql_select_db($database_check_mag, $check_mag);
$query_getPosts = "SELECT games.id_game, games.seizoen, games.date, games.type, games.hometeam, games.awayteam, teams.id_team, teams.naam_team, teams.logo, teams.adres FROM games INNER JOIN teams ORDER BY games.date DESC";
$query_limit_getPosts = sprintf("%s LIMIT %d, %d", $query_getPosts, $startRow_getPosts, $maxRows_getPosts);
$getPosts = mysql_query($query_limit_getPosts, $check_mag) or die(mysql_error());
$row_getPosts = mysql_fetch_assoc($getPosts);
And this is how i display it:
<table width="100%">
<tr>
<th scope="col" align="left">Date</th>
<th scope="col" align="left">Type</th>
<th scope="col" align="left">Game</th>
<th scope="col"> </th>
<th scope="col"> </th>
</tr>
<?php do { ?>
<tr>
<td align="left"><?php echo $row_getPosts['date']; ?></td>
<td align="left"><?php echo $row_getPosts['type']; ?></td>
<td align="left"><?php echo $row_getPosts['hometeam']; ?> - <?php echo $row_getPosts['awayteam']; ?></td>
<td><a href="games_edit.php?id_game=<?php echo $row_getPosts['id_game']; ?>">EDIT</a></td>
<td><a href="games_delete.php?id_game=<?php echo $row_getPosts['id_game']; ?>">DELETE</a></td>
</tr>
<?php } while ($row_getPosts = mysql_fetch_assoc($getPosts)); ?>
</table>
So now the echo $row_getPosts['hometeam'] and echo $row_getPosts['awayteam'] are displaying the id_team fields correctly but i want them do display the naam_team fields, which are different of course...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要加入球队表两次
每队一次
像这样:
you need to join the teams table twice
once for each team
like this: