所以我有两个表设置:
=========================
| Shares |
=========================
| user_id | other stuff |
=========================
| 1 | bla bla bla |
| 2 | bla bla bla |
=========================
=====================================
| Followers |
=====================================
| id | follower_id | following_id |
=====================================
| 1 | 7 | 1 |
| 2 | 7 | 2 |
=====================================
我想做的是创建一个显示"共享"的提要,但只针对您关注的人。理想情况下,我也希望使用一个查询来完成此操作。
这是我尝试过的一件(显然不正确的)事情。为了这个例子,假设:user_id是当前用户(或追随者)的id。
select * from shares where count(select * from followers where follower_id = :user_id and following_id = share.id) > 0
您可以使用以下内容连接表:
select s.* from shares s, followers f
where f.follower_id = :user_id
and s.user_id = f.following_id
这使用了隐式联接语法,许多人更喜欢显式变体,但在这种简单的情况下,这并不是真正必要的。