获取您关注mysql查询的用户帖子

时间:2017-04-08 15:28:12

标签: php mysql sql database

这个问题取决于我之前的问题: MySql get all rows where id = xxx and where the newest column in other table in row with same id is greater than one

我目前正在使用以下查询来获取所有公开用户的所有帖子:

select p.*
    from (select p.*,
                 (select pp.public_type
                  from tbl_post_public pp
                  where pp.post_id = p.post_id
                  order by date desc
                  limit 1
                 ) as latest_public
          from tbl_post p
          order by p.date desc
         ) p
    where latest_public < 80
    limit 10;

但不是我想只获得我喜欢的用户帖子,就像在Instagram上一样 我有以下表格来保存谁是谁:

tbl_user_follow:

id |  user     | follower_user | 
--------------------------------
 0 |  xxxxx4   | xxxxx1 (<-me) |
 1 |  xxxxx8   | xxxxx1 (<-me) |
 2 |  xxxxx3   | xxxxx6        |

所以让我们认为我是用户“xxxxx1”,现在我希望查询能够查看“xxxxx4”和“xxxxx8”的所有帖子,因为我正在考虑我之前的问题。

非常感谢你,抱歉我的英语不好:)

1 个答案:

答案 0 :(得分:0)

您没有在问题中指定表格的名称,或者在其他表格中指定字段名称,因此请调整以下内容以匹配这些名称。还要将$ user_id替换为要匹配的ID。

select p1.*
    from (select p.*,
                (select pp.public_type
                from tbl_post_public pp
                where pp.post_id = p.post_id
                order by date desc
                limit 1
                ) as latest_public
        from tbl_post p
        order by p.date desc
        ) p1
    JOIN `tbl_user_follow` f
    ON f.`user` = p1.`user`
    where latest_public < 80
    AND (f.`follower_user` = $userID OR f.`user` = $userID)
    GROUP BY f.`follower_user`, f.`user`
    limit 10;