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

标签 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”的所有帖子,因为考虑到我之前的问题,我正在关注他们。

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

最佳答案

您没有指定问题中的表名称或其他表中的字段名称,因此请调整以下内容以匹配这些名称。还将 $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;

关于php - 只获取您关注的用户的帖子 mysql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43295925/

相关文章:

mysql - 与 Oracle 的分析表查询等效的替代查询

mysql - 更新同一语句中的多个列

java - 如何在 netbeans 中查看 mysql 查询的结果

php - 对象属性中的@是什么?

php - 使用ajax发布表单数据?

mysql - 带日期和限制的查询(DQL 中可选)

mysql - 列名宽度会影响 Microsoft SQL Server/Oracle/MySQL 中的 SQL 查询性能吗?

函数的sql连接

php - 当元素通过 mysql 显示时,Jquery 事件不会触发

php - PHP substr() 的 Ruby 等价物是什么?