我有一个包含3个字段的关注/关注者表:
id , FollowingUserName,FollowedUserName
我有一张帖子的表格:
id,Post,PublishingUsername
我需要一个从post
返回某些字段的查询但“where”将在哪里:
PublishingUsernam
将与FollowedUserName
表格中的Following/Followers
匹配FollowingUserName
将登录用户名。答案 0 :(得分:0)
你正在寻找一个JOIN的样子。基本上,您希望从发布用户名=后跟用户名的帖子表中选择,以及followusername = loggedin name。
刚刚进行了攻击(因为我现在没有SQL服务器),但它可能看起来像:
SELECT * FROM Posts INNER JOIN Following ON Posts.PublishingUsername = Following.FollowedUserName WHERE FollowingUserName = LoggedInName
答案 1 :(得分:0)
如果没有JOIN
,您可以:
SELECT * FROM Posts p, Followers f
WHERE
f.FollowingUserName = 'LoggedInUserName'
AND f.FollowedUserName = p.PublishingUsername
答案 2 :(得分:0)
SELECT 'fields'
FROM TABLE1 INNER JOIN TABLE2
ON TABLE1.FollowedUserName = TABLE2.PublishingUsername
WHERE FollowingUserName = 'myvalue'
答案 3 :(得分:0)
要获得帖子:
select p.* from posts p where p.PublishingUsername in
(select FollowedUsername from followers)
and p.PublishingUsername = LOGGEDINUSER
或者您可以使用加入:
select p.* from posts p
left join followers f on f.PublishingUsername = p.PublishingUsername
and p.publishingUsername = LOGGENINUSER
答案 4 :(得分:0)
使用更清晰的加入语法
SELECT *
FROM following f
JOIN posts p ON p.PublishingUsername = f.FollowedUsername
AND f.FollowingUserName = LOGGED_IN;