我有3张桌子。
所以user01想要检索用户的所有帖子,然后是user01 +共享的所有帖子 user01
的用户编辑:我怎么能得到它?我试过这个问题:
select * from post
left join follow on post_user = follow_user
left join share on follow_user = share_user
但我只得到了第一部分。
答案 0 :(得分:1)
我不相信你可以不使用工会就能做到。性能方面,您基本上运行两个查询。确保索引了适当的字段,你应该没事。
SELECT tb_post.id, tb_post.text
FROM tb_post INNER JOIN tb_follow ON tb_post.user = tb_follow.followed_user
WHERE (((tb_follow.user)=100))
ORDER BY tb_post.id
UNION SELECT tb_post.id, tb_post.text
FROM (tb_share INNER JOIN tb_follow ON tb_share.user = tb_follow.followed_user) INNER JOIN tb_post ON tb_share.fk_post_id = tb_post.id
WHERE (((tb_follow.user)=100))
ORDER BY tb_post.id;