Select fields using Foreign keys in relational table

时间:2015-04-23 05:40:50

标签: php sql

I am trying to display topic create (username) but have to go through a relation table to retrieve it. I have already created a query that displays the creator (username) of a reply, i believe i need a sub-query but have never used one before.

What i am basically trying to do is use the foreign keys to retrieve username, i hope below explains it:

Forum_replies.topic_id >>>>> forum_topics.topic_id and forum_topics.user_id >>>> users.user_id.

The Tables are as follows:

forum_replies

  • reply_id
  • topic_id
  • user_id
  • reply_text
  • reply date

forum_topics

  • topic_id
  • category_id
  • user_id
  • topic_title
  • topic_description
  • topic_date

users

  • user_id
  • username

Here is my code which currently displays forum_topics.Topic_title, forum_replies.reply_date, forum_replies.user_id (shows username of reply creator), forum_replies.reply_text.

    $queryreply = "SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
                       forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username, forum_topics.user_id,
                       forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date
                       FROM forum_replies
                       LEFT JOIN forum_topics
                       ON forum_replies.topic_id = forum_topics.topic_id
                       LEFT JOIN users
                       ON forum_replies.user_id = users.user_id

                       ";


        $result = mysql_query($queryreply) or die (mysql_error());
        $row = mysql_fetch_array($result); 

          if(empty($row['topic_id'])){
            echo "No replies have been posted in this Topic, be the first to have your say using form below.";} ?>
        <table id="categorytable">

                <tr><td><?php echo '<b>'.$row['topic_title'].'</b>';?></b><br><br></td></tr>
                <tr><td><?php echo $row['reply_date'].' - '.$row['username'].' Replied with: ';?><br><br></td></tr>
                <tr><td><?php echo $row['reply_text'];?></td></tr>

I know mysql_* functions are deprecated but i have been asked to use them by uni staff. I would be greatful for any help. Thanks

3 个答案:

答案 0 :(得分:0)

添加另一个JOIN:

SELECT forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id,
    forum_replies.reply_text, forum_replies.reply_date, users.user_id, users.username, forum_topics.user_id,
    forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date, 
    users.user_id as topic_user_id, users.username as topic_username, 
FROM forum_replies
    LEFT JOIN forum_topics ON forum_replies.topic_id = forum_topics.topic_id
    LEFT JOIN users ON forum_replies.user_id = users.user_id
    LEFT JOIN users u2 ON forum_topics.user_id = u2.user_id

...并且如您在其他问题中所述:使用别名

答案 1 :(得分:-1)

您可以根据需要多次加入表格。所以你可以加入更多的表用户,但使用不同的ON语句。

&#13;
&#13;
SELECT 
  forum_replies.reply_id, forum_replies.topic_id, forum_replies.user_id, forum_replies.reply_text,
  forum_replies.reply_date, users.user_id, users.username, forum_topics.user_id,
  forum_topics.topic_id,forum_topics.topic_title, forum_topics.topic_date, 
  topic_creators.username
FROM forum_replies 
LEFT JOIN forum_topics ON forum_replies.topic_id = forum_topics.topic_id 
LEFT JOIN users ON forum_replies.user_id = users.user_id 
LEFT JOIN users as topic_creators ON forum_topics.user_id = topic_creators.user_id;
&#13;
&#13;
&#13;

答案 2 :(得分:-1)

另一个stackoverflow用户发给我代码,它现在工作得很好,谢谢你们的帮助。我非常感谢你的建议。如果您感兴趣的话,这是代码:

     $queryreply = "SELECT a.reply_id,a.reply_text, a.reply_date, b.topic_title, c.username AS reply_user, (SELECT username FROM users 
        WHERE user_id=b.user_id) AS topic_creator FROM forum_replies a
        LEFT JOIN forum_topics b ON a.topic_id=b.topic_id
        LEFT JOIN users c ON a.user_id=c.user_id

                       ";