1

我有一个包含 3 个字段的关注者/关注者表:

id , FollowingUserName,FollowedUserName

我有一张有帖子的桌子:

id,Post,PublishingUsername

我需要一个从帖子返回某些字段的查询

但“哪里”将是哪里:

  • From The PublishingUsernamPosts 将匹配FollowedUserNameFrom The Following/FollowersTable
  • 并且FollowingUserName将是登录的用户名。
4

5 回答 5

0

使用更简洁的 Join 语法

SELECT *
FROM following f
JOIN posts p ON p.PublishingUsername = f.FollowedUsername
AND f.FollowingUserName = LOGGED_IN;
于 2013-06-03T12:30:02.610 回答
0

没有JOIN你可以:

SELECT * FROM Posts p, Followers f
WHERE
    f.FollowingUserName = 'LoggedInUserName' 
    AND f.FollowedUserName = p.PublishingUsername
于 2013-06-03T12:27:36.957 回答
0
SELECT 'fields'    
FROM TABLE1 INNER JOIN TABLE2 
ON TABLE1.FollowedUserName = TABLE2.PublishingUsername
WHERE FollowingUserName = 'myvalue'
于 2013-06-03T12:28:37.900 回答
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
于 2013-06-03T12:29:04.463 回答
0

您正在寻找看起来像的JOIN 。基本上,您想从发布用户名 = 关注的用户名和关注用户名 = 登录名的帖子表中进行选择。

只是尝试一下(因为我现在没有 SQL 服务器),但它可能看起来像:

SELECT * FROM Posts INNER JOIN Following ON Posts.PublishingUsername = Following.FollowedUserName WHERE FollowingUserName = LoggedInName

于 2013-06-03T12:25:54.847 回答