我想顯示主題創建(用戶名),但必須通過關係表來檢索它。我已經創建了一個查詢,顯示答覆的創建者(用戶名),我相信我需要一個子查詢,但以前從未使用過。在關係表中使用外鍵選擇字段
什麼,我基本上試圖做的是使用外鍵來獲取用戶名,我希望下面的解釋它:
Forum_replies.topic_id >>>>> forum_topics.topic_id和forum_topics.user_id >>>>用戶。用戶名。
的表如下:
forum_replies
- reply_id
- topic_id
- USER_ID
- REPLY_TEXT
- 回覆日期
forum_topics
- topic_id
- CATEGORY_ID
- USER_ID
- TOPIC_TITLE
- topic_description
- topic_date
用戶
- user_id說明
- 用戶名
這是我的代碼目前顯示 forum_topics.Topic_title,forum_replies.reply_date,forum_replies.user_id(示出了回覆創建者的用戶名),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>
我知道mysql_ *函數已被棄用,但我被要求uni工作人員使用它們。我會很樂意提供任何幫助。由於
另一個stackoverflow用戶給我的代碼,它現在完美工作,謝謝你們的幫助,雖然。我非常感謝你的建議。這裏是代碼,如果您感興趣:$ queryreply =「SELECT a.reply_id,a.reply_text,a.reply_date,b.topic_title,c.username AS reply_user,(SELECT username FROM users \t \t \t WHERE user_id = b.user_id )AS topic_creator FROM forum_replies一個 \t \t \t LEFT JOIN forum_topics b關於a.topic_id = b.topic_id \t \t \t LEFT JOIN用戶C對a.user_id = c.user_id \t \t \t \t \t \t \t \t \t \t \t \t「; – phpdrivesmemad