我想显示主题创建(用户名),但必须通过关系表来检索它。我已经创建了一个查询,显示答复的创建者(用户名),我相信我需要一个子查询,但以前从未使用过。在关系表中使用外键选择字段
什么,我基本上试图做的是使用外键来获取用户名,我希望下面的解释它:
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