连接和可能的子查询
我有 3 个表:user
、'user_friends' 和 'blog_post'
架构类似于以下内容:
user
id | username
-------------------
1 | jim
-------------------
2 | mary
-------------------
3 | george
-------------------
4 | julie
user_friends
user_id | friend_id
----------------------
1 | 2
----------------------
2 | 3
----------------------
2 | 4
blog_posts
id | title | user_id
------------------------------
1 | test_1 | 1
------------------------------
2 | test_2 | 2
------------------------------
3 | test_3 | 3
------------------------------
4 | test_4 | 4
好的,您可以看到每个用户都发表了一篇博客文章。用户有“朋友”。我想做的是显示每个用户朋友的博客帖子。
因此,如果我想查看吉姆朋友的帖子,它应该显示玛丽的 test_2 帖子。如果我想查看玛丽的朋友帖子,它应该显示乔治和朱莉的帖子 test_3 和 test_4
有没有简单的方法可以做到这一点?我可以使用子选择吗?
非常感谢
I have 3 tables: user
, 'user_friends' and 'blog_post'
The schema is similar to the following:
user
id | username
-------------------
1 | jim
-------------------
2 | mary
-------------------
3 | george
-------------------
4 | julie
user_friends
user_id | friend_id
----------------------
1 | 2
----------------------
2 | 3
----------------------
2 | 4
blog_posts
id | title | user_id
------------------------------
1 | test_1 | 1
------------------------------
2 | test_2 | 2
------------------------------
3 | test_3 | 3
------------------------------
4 | test_4 | 4
Ok, so you can see each user has made a blog post. Users have 'friends'. What I'm looking to do, is display each users friends blog posts.
So, If I'd like to see Jims friends posts, it should show mary's post of test_2. If I want to see mary's friends posts, it should show george and julie's posts of test_3 and test_4
Is there an easy way of doing this? Could I use sub-selects?
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,您可以使用子选择。
像这样的东西应该有效:
Yes you can use a sub-selection.
Something like this should work :
可能就这么简单(带有 JOIN 的变体):
Could be as simple as that (variant with a JOIN):
末尾的数字可以是您要查找的任何用户 ID。
the number at the end can be whatever user ID you are looking for.
您可以使用IN
You can use IN
尝试这个请求:
Try this request: