[英]Joining two tables to match integers to get a username
我在弄清楚如何連接兩個表並從不同的數據庫表中SELECT
不同的行時遇到了麻煩。
我想添加我的users
表以將id
與post_creator
匹配,然后從users
表中獲取ID和post_creator編號均匹配的users
username
。
因此,如果我的用戶表的ID為10,而那是吉姆的用戶名。 如果post_creator值為10,我希望找到Jim並將其回顯。
現在,這正在殺死代碼,我不知道自己在做什么錯。
while ($row = $stmt->fetch()) {
//added in topic title variable
$topic_title;
// foreach($stmt as $row) {
//Prepared SELECT stmt to get forum posts
$stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
if($stmt2===false) {
die();
} else {
//var_dump($stmt2);
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
if (!$stmt2) {
throw new Exception($con->error);
}
}
$num_rows2 = $stmt2->num_rows;
if($num_rows2) {
$count2=0;
while($stmt2->fetch()) {
$count2++;
$post_id;
$post_category_id;
$post_topic_id;
$post_creator;
$post_content;
$post_date = fixDate($post_date);
//$post_date;
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$topic_title. "Post ".$count2."<br />
by ".$post_creator." - " .$post_date. "<hr />" . $post_content ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
您可以聯接表...諸如此類(聯接用戶,類別和主題)
SELECT forum_posts.id post_id, forum_posts.category_id post_category_id, forum_posts.topic_id post_topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date, forum_post_categories.category_name, forum_post_topics.topic_name, users.id user_id, users.username user_username FROM forum_posts LEFT JOIN users ON users.id = forum_posts.post_creator RIGHT JOIN forum_post_categories ON forum_post_categories.id = forum_posts.category_id RIGHT JOIN forum_post_topics ON forum_post_topics.id = forum_posts.topic_id WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?
注意:這未經測試。
select u.*,fp.* from users u inner join forum_posts fp u.id=fp.post_creator
where fp.category_id=? AND fp.topic_id=? AND fp.post_creator=? AND u.id=?
更新的查詢:
select u.user_name,fp.post_creator from
users u inner join forum_posts fp u.id=fp.post_creator
選中此更新的版本,它只會為您提供user_name和post_creator
嘗試刪除此代碼部分
if($stmt2===false) {
die();
} else { }
只需:
$stmt2 = $con->prepare("SELECT forum_posts.id, forum_posts.category_id, forum_posts.topic_id, forum_posts.post_creator, forum_posts.post_content, forum_posts.post_date users.id
FROM forum_posts LEFT JOIN users WHERE forum_posts.category_id=? AND forum_posts.topic_id=? AND forum_posts.post_creator=? AND users.id=?");
$stmt2->bind_param("ii", $cid, $tid);
$stmt2->execute();
$stmt2->store_result();
$stmt2->bind_result($post_id, $post_category_id, $post_topic_id, $post_creator, $post_content, $post_date);
if (!$stmt2) {
throw new Exception($con->error);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.