一张表有 2 个字段引用同一张表

发布于 2025-01-08 19:24:00 字数 1988 浏览 2 评论 0原文

我有一个表(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">&nbsp;</th>
<th scope="col">&nbsp;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哆啦不做梦 2025-01-15 19:24:00

您需要加入球队表两次
每队一次
像这样:

从游戏g中选择g.id_game、homet.naam_team、awayt.naam_team,
主队、客队,其中 g.hometeam = homet.id_team 且
g.awayteam =awayt.id_team;

you need to join the teams table twice
once for each team
like this:

select g.id_game, homet.naam_team, awayt.naam_team from games g,
teams homet, teams awayt where g.hometeam = homet.id_team and
g.awayteam = awayt.id_team;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文