【问题标题】:get only posts of users you follow mysql query仅获取您关注 mysql 查询的用户的帖子
【发布时间】:2017-04-08 15:28:11
【问题描述】:

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

非常感谢你,对不起我的英语不好:)

【问题讨论】:

  • 您使用的是哪个数据库?您标记了 mysql 和 sql-server。
  • 如果您之前的问题是关于 MySQL 的,那么我认为这个也是。请不要在您的问题上添加多余的标签。
  • 对不起...我正在使用 mysql
  • 我看到的一个问题是您为多个表提供了相同的别名。除此之外,您只需要使用 follower_user 对表(名称?)进行连接。
  • 好的,我更新了我的问题。表名是“tbl_user_follow”。我是 sql 新手 :) 如果你知道这个查询,你能回答吗?

标签: php mysql sql database


【解决方案1】:

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

【讨论】:

  • 非常感谢!有用!你能告诉我如何获得自己的帖子吗?所以像“AND f.follower_user = $userID OR p1.'user' = $userID”
  • 只需 select * from tbl_post where user = $user_ID; 另外,如果它对你有用,请接受答案。
  • 呵呵是的,我会接受答案。但是您能否使用 'select * from tbl_post where user = $user_ID' 编写完整的查询?我是 mysql 中的菜鸟 :)
  • 我已经在 'AND f.follower_user = '$userID' 之后使用 'OR p1.user = '$userID'' 进行了尝试。但这会给出一排六次
  • 这是对您帖子的完整查询。您想将自己的帖子添加到其他查询吗?不知道您为什么要在同一个查询中使用它。
猜你喜欢
  • 1970-01-01
  • 2015-11-08
  • 2023-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多