在下面的表中,在用户的mysql中/正在订阅其他用户(在'follow'表中相关)我希望获得给定用户订阅的用户发布的帖子:
表1:按照
|-----------------------------------------|
| id | uid | friends |
|-----------------------------------------|
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
|-----------------------------------------|
表2:帖子
|-----------------------------------------------------|
| id | uid | posts | date |
|-----------------------------------------------------|
| 1 | 1 | hai.. | 2013-07-08 01:56:09 |
| 2 | 5 | awesome | 2013-07-08 11:45:50 |
| 3 | 2 | greate!! | 2013-07-09 21:13:29 |
| 4 | 3 | himm.. | 2013-07-10 12:06:10 |
| 5 | 2 | super.. | 2013-07-10 14:50:09 |
|-----------------------------------------------------|
table3:用户
|---------------------------|
| uid | name |
|---------------------------|
| 1 | ram |
| 2 | syed |
| 3 | seeta |
|---------------------------|
例如: 鉴于使用uid 1的用户,使用uid 2和3跟踪这两个用户,我想显示我的帖子和跟随者的最新帖子。
结果如下:
2 posted
super..
time:2013-07-10 14:50:09
3 posted
himm..
time:2013-07-10 12:06:10
2 posted
greate!!
time:2013-07-09 21:13:29
1 posted
hai..
time:2013-07-08 01:56:09
答案 0 :(得分:1)
假设您的用户ID为5:
SELECT * FROM posts WHERE posts.uid IN (SELECT follow.friends FROM follow WHERE follow.uid=5)
或者您可以加入表格:
SELECT posts.* FROM posts JOIN follow ON posts.uid = follow.friends WHERE follow.uid=5
如果你想看到自己的帖子:
SELECT * FROM posts WHERE posts.uid=5 OR posts.uid IN (SELECT follow.friends FROM follow WHERE follow.uid=5)
答案 1 :(得分:0)
我不确定你的表是否在sql中,但是如果它们是,我认为获得数据的最佳方法是使用LEFT JOIN,如下所示:
SELECT * FROM follow
JOIN posts
ON (follow.friends = posts.uid OR follow.uid = posts.uid)
WHERE follow.uid = {user_uid}
其中{user_id}是您想要的用户ID。
如果是这种情况,请进一步查看联接(f {。在tizag)并离开联接。