将第二个数据库表添加到查询中,并输出与另一个具有相同名称的列


Adding a second database table to a query and outputting a column that has the same name as another

我有下面的查询,我很难找出在数据库表中添加的最佳方法。我想把表forum_categories加进去然后从中选择id。我不确定如何在查询中拥有两个相同的列名,但这是我需要从该表中获得的唯一字段。

如何添加forum_categories到这个查询.

$query2 = mysqli_query($con,"SELECT * FROM forum_topics ORDER BY topic_reply_date DESC LIMIT 3")

然后只从它获取id,并能够输出它具有相同的列名?

<?php
    $con = mysqli_connect("localhost", "", "", "");
    $query2 = mysqli_query($con,"SELECT * FROM forum_topics ORDER BY topic_reply_date DESC LIMIT 3")
    or die ("Query2 failed: %s'n".($query2->error));
    $numrows2 = mysqli_num_rows($query2);
    if($numrows2 > 0){
    $topics .= "<table width='100%' style='border-collapse: collapse;'>";
    //Change link once discussion page is made
    $topics .= "<tr style='background-color: #dddddd;'><td>Topic Title</td><td width='65' align='center'>Replies</td><td width='65' 
    align='center'>Views</td></tr>";
    $topics .= "<tr><td colspan='3'><hr /></td></tr>";
    while($row2 = mysqli_fetch_assoc($query2)){
        $tid = $row2['id'];
        $title = $row2['topic_title'];
        $views = $row2['topic_views'];
        $date = $row2['topic_date'];
        $creator = $row2['topic_creator'];
        $topics .= "<tr><td><a href='forum_view_topic.php?tid=".$tid."'>".$title."</a><br /><span class='post_info'>Posted 
        by: ".$creator." on ".$date."</span></td><td align='cener'>0</td><td align='center'>".$views."</td></tr>";
        $topics .= "<tr><td colspan='3'><hr /></td></tr>";
    }
    $topics .="</table>";
    echo $topics;
    } else {
    echo "<p>There are no topics in this category yet.</p>";
    }
    ?>

更正后的SQL查询:

SELECT t.*, c.id AS cid FROM forum_topics AS t, forum_categories AS c ORDER BY t.topic_reply_date DESC LIMIT 3
相关文章: