簡體   English   中英

MySQL預備語句聯接查詢

[英]MySQL Prepared Statement Join Query

您好,我有以下表格;

表-帖子

PostId,
Email,
ForumId
DatePosted,
Likes,
Dislikes

表-論壇

ForumId,
ForumTitle,
ForumPostText,
PostDate,
Views

我想從“ posts ”表中獲取大多數信息,並從“ forum ”表中獲取forumTitle,這兩個表共享forumsId。 這將允許使用特定電子郵件地址的用戶查看他們在哪個論壇上發布。 我對MySQL加入沒有太多經驗,對任何幫助都深表感謝。

$stmt = $mysqli->prepare(" SELECT posts.PostId, posts.Email, posts.ForumId, forum.ForumId, forum.ForumTitle FROM posts LEFT JOIN forum ON forum.ForumId = posts.ForumId WHERE posts.Email = ?  ");
$stmt->bind_param('s', $usr);
$stmt->execute();
$stmt->bind_result($ForumId,$DatePosted,$Likes,$Dislikes,$Title);
$stmt->fetch();

您可以使用自我加入

SELECT posts.PostId, posts.Email, posts.ForumId, forum.ForumId, forum.ForumTitle 
FROM posts,
forum 
WHERE 
forum.ForumId = posts.ForumId 
AND 
posts.Email = ?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM