我有两张桌子:
俱乐部
|---------------------|------------------|
| id | name |
|---------------------|------------------|
| 1 | Arsenal |
|---------------------|------------------|
| 2 | Chelsea |
|---------------------|------------------|
| 3 | Fulham |
|---------------------|------------------|
| 4 | Leeds |
|---------------------|------------------|匹配
|---------------------|------------------|------------------|
| id | home | away |
|---------------------|------------------|------------------|
| 1 | 1 | 3 |
|---------------------|------------------|------------------|
| 2 | 2 | 4 |
|---------------------|------------------|------------------|解释:表中的"home“和"away”列中的数字链接到de Club表中的id。
现在,我想(使用php)回显如下:
|---------------------|------------------|
| home | away |
|---------------------|------------------|
| Arsenal | Fulham |
|---------------------|------------------|
| Chelsea | Leeds |
|---------------------|------------------|解释:,当单击“工作组名称”时,我想打开包含“团队信息”的club.php页面。
我试过以下几种方法:
<?php
$sql = "SELECT * FROM matches JOIN clubs ON matches.home AND matches.away = clubs.id"
$rs_result = $conn->query($sql);
?>
<div style="overflow-x:auto">
<table>
<tr><th><strong>Home</strong></th><th><strong>Away</strong></th><tr>
<?php
while($row = $rs_result->fetch_assoc()) {
?>
<tr>
<td><a href="club.php?id=<?php echo $row['home'];?>"><? echo $row['home']; ?></a></td>
<td><a href="club.php?id=<?php echo $row['away'];?>"><? echo $row['away']; ?></a></td>
</tr>
<?php } ?>
</table></div>它呼应了以下情况:
|---------------------|------------------|
| home | away |
|---------------------|------------------|
| 1 | 3 |
|---------------------|------------------|
| 2 | 4 |
|---------------------|------------------|我的问题:如何在表中显示俱乐部名称,而在“href”链接中传递俱乐部id?我试过$row1等等,但是它不起作用。我需要哪条SQL语句?加入吗?内部加入?左加入?
事先非常感谢!
发布于 2020-04-16 12:38:13
我认为这是可行的-我对你的问题做了一个调整。如果您对查询(匹配)的左侧感兴趣,并且不关心俱乐部是否已填充,则可以使用左联接。如果你有1和5的主场客场,我们知道1是阿森纳,但是在俱乐部里没有5人的参赛资格。左连接将显示它,内部连接将不会显示。
<?php
$sql = "SELECT home,away,homeclub.name as homename,awayclub.name as awayname FROM matches
JOIN clubs as homeclub ON matches.home = homeclub.id
JOIN clubs as awayclub ON matches.away = awayclub.id"
$rs_result = $conn->query($sql);
?>
<div style="overflow-x:auto">
<table>
<tr><th><strong>Home</strong></th><th><strong>Away</strong></th><tr>
<?php
while($row = $rs_result->fetch_assoc()) {
?>
<tr>
<td><a href="club.php?id=<?php echo $row['home'];?>"><? echo $row['homename']; ?></a></td>
<td><a href="club.php?id=<?php echo $row['away'];?>"><? echo $row['awayname']; ?></a></td>
</tr>
<?php } ?>
</table>
</div>https://stackoverflow.com/questions/61250136
复制相似问题