【问题标题】:SELECT posts, its last 3 comments and count of all comments选择帖子,最后 3 条评论和所有评论的计数
【发布时间】:2015-04-17 06:02:03
【问题描述】:

选择帖子,最后 3 个 cmets 和所有 cmets 的计数 我的代码:

SELECT p.*, u.id, u.username username, u.usersurname usersurname, u.usermainphoto userphoto, GROUP_CONCAT(c.text SEPARATOR 'a!k@h#md%o^v&') commenttext,GROUP_CONCAT(c.likes SEPARATOR '-') commentlikes,GROUP_CONCAT(c.dislikes SEPARATOR '-') commentdislikes, GROUP_CONCAT(c.commentdate) commentdate, GROUP_CONCAT(u2.username) commentauthorname, GROUP_CONCAT(c.anonim) commentanonym, GROUP_CONCAT(c.id) commentid, GROUP_CONCAT(u2.id) commentauthorid, GROUP_CONCAT(u2.usersurname) commentauthorsurname, GROUP_CONCAT(u2.usermainphoto) commentauthorphoto, GROUP_CONCAT(c.commentphotoid) commentphotoid
FROM posts p
LEFT JOIN comments c ON c.post = p.postid AND c.commentdel=0
LEFT JOIN users u ON u.id = p.postauthorid
LEFT JOIN users u2 ON u2.id = c.author
WHERE p.postwallid = :id AND p.postdel=0
GROUP BY postid
ORDER BY postid DESC

它给了我所有的 cmets,但我只需要 3 个

【问题讨论】:

  • 首先,对于 GROUP BY,您应该只选择 GROUP BY 部分中的原始字段,或者您选择的字段应该是聚合函数(最小值、最大值、总和等)。 .)。然后,我在您的查询中看不到 COUNT 函数,因此您将无法计算 cmets。

标签: php sql select pdo comments


【解决方案1】:

您需要某个时间戳或计数器来确定您想要的三个 cmet。在下面的尖括号之间添加该列名称。

SELECT p.*, u.id, u.username username, u.usersurname usersurname, u.usermainphoto userphoto, GROUP_CONCAT(c.text SEPARATOR 'a!k@h#md%o^v&') commenttext,GROUP_CONCAT(c.likes SEPARATOR '-') commentlikes,GROUP_CONCAT(c.dislikes SEPARATOR '-') commentdislikes, GROUP_CONCAT(c.commentdate) commentdate, GROUP_CONCAT(u2.username) commentauthorname, GROUP_CONCAT(c.anonim) commentanonym, GROUP_CONCAT(c.id) commentid, GROUP_CONCAT(u2.id) commentauthorid, GROUP_CONCAT(u2.usersurname) commentauthorsurname, GROUP_CONCAT(u2.usermainphoto) commentauthorphoto, GROUP_CONCAT(c.commentphotoid) commentphotoid
FROM posts p
LEFT JOIN comments c ON c.post = p.postid AND c.commentdel = 0
LEFT JOIN users u ON u.id = p.postauthorid
LEFT JOIN users u2 ON u2.id = c.author
WHERE p.postwallid = :id AND p.postdel = 0
    and (
        select count(*) from comments as c2
        where c2.postid = p.postid and c2.commentdel = 0
        and c2.<timestamp> <= c.timestamp
    ) < 3
GROUP BY postid
ORDER BY postid DESC

编辑:我没有添加所有 cmets 的计数。我认为您可以轻松地将其与选择列表中的另一个子查询一起添加,但我知道 MySQL 人不太喜欢子查询。

(
select count(*) from comments as c2
where c2.postid = c.postid and c2.commentdel = 0
) as comment_count

【讨论】:

    【解决方案2】:

    我将 SELECT count(postid) 添加为 all_cmets,最后添加 LIMIT 0, 3

    SELECT count(postid) as all_comments, p.*, u.id, u.username username, u.usersurname usersurname, u.usermainphoto userphoto, GROUP_CONCAT(c.text SEPARATOR 'a!k@h#md%o^v&') commenttext,GROUP_CONCAT(c.likes SEPARATOR '-') commentlikes,GROUP_CONCAT(c.dislikes SEPARATOR '-') commentdislikes, GROUP_CONCAT(c.commentdate) commentdate, GROUP_CONCAT(u2.username) commentauthorname, GROUP_CONCAT(c.anonim) commentanonym, GROUP_CONCAT(c.id) commentid, GROUP_CONCAT(u2.id) commentauthorid, GROUP_CONCAT(u2.usersurname) commentauthorsurname, GROUP_CONCAT(u2.usermainphoto) commentauthorphoto, GROUP_CONCAT(c.commentphotoid) commentphotoid
    FROM posts p
    LEFT JOIN comments c ON c.post = p.postid AND c.commentdel=0
    LEFT JOIN users u ON u.id = p.postauthorid
    LEFT JOIN users u2 ON u2.id = c.author
    WHERE p.postwallid = :id AND p.postdel=0
    GROUP BY postid
    ORDER BY postid DESC
    LIMIT 0, 3
    

    【讨论】:

    • 在这种情况下,你限制了 POSTS,但我需要限制 cmets。
    猜你喜欢
    • 2015-03-04
    • 1970-01-01
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多