php - MYSQL 查询 - 获取当前用户的帖子和用户正在关注的帖子

标签 php mysql

我正在尝试查询以获取当前用户的帖子和他关注的人的帖子。就像大多数社交网络在其主页中所做的那样。

我知道如何查询当前用户的所有帖子,但我很难获得用户正在关注的帖子。

我看到了另一个问题 Mysql select query for getting current user post and followed friend post

但那里的答案并没有真正帮助我......

到目前为止,这是我的查询:

SELECT P.id, 
P.caption,
P.date,
U.id,
U.fullname,
U.username,
F.IdOtherUser
FROM USERS AS U
INNER JOIN Activity AS F
ON U.id = F.id
INNER JOIN Posts AS P 
ON P.id = U.id OR P.id = F.IdOtherUser
WHERE P.id = 145
ORDER BY P.id DESC

145 = 当前用户。

Activity.IdOtherUser = 用户“145”正在关注

Activity.id = 将是 '145' 当前用户

如果有人能帮我解决这个问题,我将不胜感激!

似乎无法完全理解它,因为我对 MYSQL 很陌生......

修复

(SELECT P.id as postid, 
        P.caption,
        P.date,
        U.id as userid,
        U.fullname,
        U.username,
        coalesce(Activity.LikeCNT,0),
        Activity.CurrentUserLiked
        FROM USERS AS U
        INNER JOIN Posts AS P 
        ON P.id = U.id
        LEFT JOIN (SELECT COUNT(DISTINCT Activity.uuidPost) LikeCNT, Activity.uuidPost, Activity.id, sum(CASE WHEN Activity.id = 145 then 1 else 0 end) as CurrentUserLiked
        FROM Activity Activity
        WHERE type = 'like' 
        GROUP BY Activity.uuidPost) Activity
        ON Activity.uuidPost = P.uuid
        AND Activity.id = U.id
        WHERE U.id = 145)
UNION
(SELECT P.id, 
        P.caption,
        P.date,
        U.id,
        U.fullname,
        U.username,
        coalesce(Activity.LikeCNT,0),
        Activity.CurrentUserLiked
        FROM Activity AS A
        INNER JOIN USERS AS U 
        ON A.IdOtherUser=U.id
        INNER JOIN Posts AS P 
        ON P.id = U.id
        LEFT JOIN (SELECT COUNT(DISTINCT Activity.uuidPost) LikeCNT, Activity.uuidPost, Activity.id, sum(CASE WHEN Activity.id = 145 then 1 else 0 end) as CurrentUserLiked
    FROM Activity Activity
    WHERE type = 'like' 
    GROUP BY Activity.uuidPost) Activity
    ON Activity.uuidPost = P.uuid
    AND Activity.id = U.id
    WHERE A.id = 145)

最佳答案

要获取给定用户 (id=145) 的所有帖子及其关注帖子的所有用户,以及每个帖子的用户详细信息,我将重写查询以使用 union 而不是或,从而简化了逻辑。第一个选择获取给定用户的帖子,第二个选择获取它关注的用户的帖子:

(SELECT P.id as postid, 
       P.caption,
       P.date,
       U.id as userid,
       U.fullname,
       U.username,
FROM USERS AS U
INNER JOIN Posts AS P ON P.userid = U.id
WHERE U.id = 145)
UNION
(SELECT P.id, 
       P.caption,
       P.date,
       U.id,
       U.fullname,
       U.username,
FROM Activity AS A
INNER JOIN USERS AS U ON A.IdOtherUser=U.id
INNER JOIN Posts AS P ON P.userid = U.id
WHERE A.id = 145)
ORDER BY postid DESC

假设:

  1. Activity.id 字段表示关注其他用户的用户。如果不是,则需要将字段名称更改为合适的名称。

  2. 发明了 Posts 表的 userid 字段,表示发布帖子的用户。请在其位置使用正确的字段名称。

关于php - MYSQL 查询 - 获取当前用户的帖子和用户正在关注的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40286026/

相关文章:

php - Symfony的generate :crud command?生成的twig View 文件在哪里

javascript - 淡出电子邮件表格

php - MySQL重复条目错误,即使它不是重复条目

php - 在 codeigniter 的 session 中包含所有用户详细信息是否好?

MySQL按其他表行排序

mysql - Rails 将 mysql tinyint(1) 视为 bool 值 - 但我希望它是一个数字

php - 如何检查网站是否正常运行并长期存储结果

并列情况下的 MySQL 排名

python - 尝试安装 mysql 时获取 "ERROR: Command errored out with exit status 1:"

javascript - Json 编码 php mysql 查询