【问题标题】:How to echo results from SQL table correctly using JOIN operator?如何使用 JOIN 运算符正确回显 SQL 表中的结果?
【发布时间】:2020-04-16 12:21:07
【问题描述】:

我有两张桌子:

俱乐部

    |---------------------|------------------|
    |          id         |       name       |
    |---------------------|------------------|
    |           1         |      Arsenal     |
    |---------------------|------------------|
    |           2         |      Chelsea     |
    |---------------------|------------------|
    |           3         |      Fulham      |
    |---------------------|------------------|
    |           4         |      Leeds       |
    |---------------------|------------------|

匹配项

    |---------------------|------------------|------------------|
    |          id         |       home       |        away      |
    |---------------------|------------------|------------------|
    |           1         |        1         |         3        |
    |---------------------|------------------|------------------|
    |           2         |        2         |         4        |
    |---------------------|------------------|------------------|

解释: Matches 表中“home”和“away”列中的数字链接到 de Clubs 表中的 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        |
|---------------------|------------------|

我的问题:如何在表格中显示俱乐部名称,但在“a href”链接中传递俱乐部 ID? 我试过 $row[1] 等等,但它不起作用。我需要哪个 SQL 语句?加入?内部联接?左加入?

非常感谢!

【问题讨论】:

  • 那里的标签太多了...您是否遇到了 php、html、sql 或 echo 的问题?

标签: php html sql join echo


【解决方案1】:

我相信这应该可行 - 我对您的查询进行了调整。 当您对查询的左侧(匹配项)感兴趣并且不关心是否尚未填充俱乐部时,您可以使用左连接。 F ex,如果您有 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    • 2023-03-15
    • 2021-10-12
    相关资源
    最近更新 更多