连接和可能的子查询

发布于 2024-12-12 01:35:55 字数 890 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

爱要勇敢去追 2024-12-19 01:35:55

是的,您可以使用子选择。
像这样的东西应该有效:

SELECT * FROM blog_posts WHERE user_id in 
(SELECT friend_id FROM user_friend WHERE user_id=1)

Yes you can use a sub-selection.
Something like this should work :

SELECT * FROM blog_posts WHERE user_id in 
(SELECT friend_id FROM user_friend WHERE user_id=1)
吖咩 2024-12-19 01:35:55

可能就这么简单(带有 JOIN 的变体):

SELECT bp.*
FROM   user_friends uf
JOIN   blob_posts bp ON bp.user_id = uf.friend_id
WHERE  uf.user_id = my_id

Could be as simple as that (variant with a JOIN):

SELECT bp.*
FROM   user_friends uf
JOIN   blob_posts bp ON bp.user_id = uf.friend_id
WHERE  uf.user_id = my_id
温柔戏命师 2024-12-19 01:35:55
Select *
From blog_posts
Where user_id in
    (Select friend_id
    From user_friends
    where user_id = 1);

末尾的数字可以是您要查找的任何用户 ID。

Select *
From blog_posts
Where user_id in
    (Select friend_id
    From user_friends
    where user_id = 1);

the number at the end can be whatever user ID you are looking for.

樱花细雨 2024-12-19 01:35:55

您可以使用IN

SELECT * FROM blog_posts WHERE user_id IN (
SELECT friend_id FROM user_friends WHERE user_id = X)

You can use IN

SELECT * FROM blog_posts WHERE user_id IN (
SELECT friend_id FROM user_friends WHERE user_id = X)
国际总奸 2024-12-19 01:35:55
SELECT bp.* 
FROM blog_posts bp
INNER JOIN user_friends uf ON bp.user_id = uf.friend_id
INNER JOIN user u ON u.id = uf.user_id
WHERE u.username = "jim"
SELECT bp.* 
FROM blog_posts bp
INNER JOIN user_friends uf ON bp.user_id = uf.friend_id
INNER JOIN user u ON u.id = uf.user_id
WHERE u.username = "jim"
寄人书 2024-12-19 01:35:55

尝试这个请求:

SELECT bp.title
FROM blog_posts bp
JOIN user_friends uf ON (bp.user_id = uf.friend_id)
WHERE uf.user_id = 1;

Try this request:

SELECT bp.title
FROM blog_posts bp
JOIN user_friends uf ON (bp.user_id = uf.friend_id)
WHERE uf.user_id = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文