查询将某些结果的数量加倍

时间:2019-01-29 09:39:40

标签: mysql sql

我在此查询中尝试显示结果并计算每个主题的答复数量。结果有时会使答复数量加倍。而不是显示10,而是显示20。在查询中缺少我想要的东西吗?

SELECT      
        count(replies.reply_topic) as replyCount,
        topics.topic_id,
        topics.topic_subject,
        topics.topic_date,
        topics.topic_cat,
        topics.topic_picture,
        topics.topic_creator,
        topics.topic_likes,
        users.user_id,
        users.username,
        profile.profile_id,
        profile.profile_pictureMain,
        profile.profile_users,
        savelink.saveLink_id,
        savelink.saveUser_id,
        savelink.link_id,
        votelikes.voteLikes_user,
        votelikes.voteLikes_topic,
        votelikes.voteLikes_hot,
        votelikes.voteLikes_cold
        FROM
            topics
        LEFT JOIN
            replies
        ON
            replies.reply_topic = topics.topic_id
        LEFT JOIN
            users
        ON
            topics.topic_creator = users.user_id
        LEFT JOIN
            profile
        ON
            profile.profile_users = users.user_id
        LEFT JOIN
            savelink
        ON
            savelink.link_id = topics.topic_id
        LEFT JOIN
            votelikes
        ON
            votelikes.voteLikes_topic = topics.topic_id
        GROUP BY
            topics.topic_id
        ORDER BY
            topics.topic_date DESC 
        LIMIT ?, ?

这是我的回复表:其中reply_topic也是已回复主题的ID,它显示的主题是它的两倍。

reply_id   reply_content    reply_topic    reply_by

1           stuff            45             34
2           more stuff       76             21
3           more             45             75

像这样显示答复:

<i class="fa fa-comments"></i> '.$row['replyCount'].' Comments</a>

,而不是显示(2)Reply_topic 45的结果,而是显示(4) 等等。

1 个答案:

答案 0 :(得分:0)

您可以使用count(distinct replies.reply_topic)

SELECT      
        count(distinct replies.reply_topic) as replyCount,
        topics.topic_id,
        topics.topic_subject,
        topics.topic_date,
        topics.topic_cat,
        topics.topic_picture,
        topics.topic_creator,
        topics.topic_likes,
        users.user_id,
        users.username,
        profile.profile_id,
        profile.profile_pictureMain,
        profile.profile_users,
        savelink.saveLink_id,
        savelink.saveUser_id,
        savelink.link_id,
        votelikes.voteLikes_user,
        votelikes.voteLikes_topic,
        votelikes.voteLikes_hot,
        votelikes.voteLikes_cold
        FROM
            topics
        LEFT JOIN
            replies
        ON
            replies.reply_topic = topics.topic_id
        LEFT JOIN
            users
        ON
            topics.topic_creator = users.user_id
        LEFT JOIN
            profile
        ON
            profile.profile_users = users.user_id
        LEFT JOIN
            savelink
        ON
            savelink.link_id = topics.topic_id
        LEFT JOIN
            votelikes
        ON
            votelikes.voteLikes_topic = topics.topic_id
        GROUP BY
            topics.topic_id
        ORDER BY
            topics.topic_date DESC 
        LIMIT ?, ?