简体   繁体   中英

MySQL Prepared Statement Join Query

Hello all i have the following tables;

Table - posts

PostId,
Email,
ForumId
DatePosted,
Likes,
Dislikes

Table - forum

ForumId,
ForumTitle,
ForumPostText,
PostDate,
Views

I am wanting to pull most information from the ' posts ' table and the forumTitle from the ' forum ' table, both tables share forumId. This will allow the user of a certain email address to see which forum they have posted on. I do not have a great deal of experience with MySQL joins any help greatly appreciated.

$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();

You can use the self join

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM