[英]How to echo results from SQL table correctly using JOIN operator?
I have two tables:我有两张桌子:
Clubs俱乐部
|---------------------|------------------|
| id | name |
|---------------------|------------------|
| 1 | Arsenal |
|---------------------|------------------|
| 2 | Chelsea |
|---------------------|------------------|
| 3 | Fulham |
|---------------------|------------------|
| 4 | Leeds |
|---------------------|------------------|
Matches火柴
|---------------------|------------------|------------------|
| id | home | away |
|---------------------|------------------|------------------|
| 1 | 1 | 3 |
|---------------------|------------------|------------------|
| 2 | 2 | 4 |
|---------------------|------------------|------------------|
Explanation: the numbers in the columns "home" and "away" in the Matches table linking to the the id in de Clubs table.解释: Matches 表中“home”和“away”列中的数字链接到 de Clubs 表中的 id。
Now I want to echo (with php) the following:现在我想回显(使用 php)以下内容:
|---------------------|------------------|
| home | away |
|---------------------|------------------|
| Arsenal | Fulham |
|---------------------|------------------|
| Chelsea | Leeds |
|---------------------|------------------|
Explanation: when clicking on the teamname I want to open the club.php page with the teaminfo.说明:点击球队名称时,我想打开带有球队信息的俱乐部。php 页面。
I've tried the following:我尝试了以下方法:
<?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>
It is echoing the following:它呼应了以下内容:
|---------------------|------------------|
| home | away |
|---------------------|------------------|
| 1 | 3 |
|---------------------|------------------|
| 2 | 4 |
|---------------------|------------------|
My question: How can I show the clubnames in the table but passing the club id in the "a href" link?我的问题:如何在表格中显示俱乐部名称,但在“a href”链接中传递俱乐部 ID? I've tried $row[1] and so on but it doesnt work.我试过 $row[1] 等等,但它不起作用。 Which SQL statement do I need?我需要哪个 SQL 声明? JOIN?加入? INNER JOIN?内部联接? LEFT JOIN?左加入?
Many thanks in advance!提前谢谢了!
I believe this should work - I made an adjustment to your query.我相信这应该可行 - 我对您的查询进行了调整。 You can use left joins when you are interested in the left side of the query (matches) and do not care if the clubs have not been populated.当您对查询的左侧(匹配项)感兴趣并且不关心是否尚未填充俱乐部时,您可以使用左连接。 F ex if you have a home - away of 1 and 5. We know that 1 is Arsenal but there is no entry for 5 in clubs. F ex,如果您有 1 和 5 的主客场。我们知道 1 是阿森纳,但在俱乐部中没有 5 的条目。 Left join will display it, inner join will not.左连接会显示它,内连接不会。
<?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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.