【问题标题】:weird problem with select count from multiple tables (with joins)从多个表中选择计数的奇怪问题(带有连接)
【发布时间】:2011-05-04 12:24:45
【问题描述】:

我对以下查询有一个奇怪的问题,它工作正常, 其中的计数部分让我知道给定“提示”上的 cmets 数量

我正在尝试添加一个类似的计数来获取每个提示的“投票”数量,以下是查询:

SELECT h.*
  , h.permalink AS hintout_permalink
  , hi.hinter_name
  , hi.permalink
  , hf.user_id AS followed_hid
  , ci.city_id, ci.city_name, co.country_id, co.country_name, ht.thank_id
  , COUNT(hc.comment_id) AS commentsCount 
FROM hintouts AS h 
INNER JOIN hinter_follows AS hf ON h.hinter_id = hf.hinter_id 
INNER JOIN hinters AS hi ON h.hinter_id = hi.hinter_id 
LEFT JOIN cities AS ci ON h.city_id = ci.city_id 
LEFT JOIN countries as co ON h.country_id = co.country_id 
LEFT JOIN hintout_thanks AS ht ON (h.hintout_id = ht.hintout_id 
  AND ht.thanker_user_id = 1)
LEFT JOIN hintout_comments AS hc ON hc.hintout_id = h.hintout_id 
WHERE hf.user_id = 1 
GROUP BY h.hintout_id 

我尝试将以下内容添加到选择部分:

COUNT(ht2.thanks_id) AS thanksCount 

以及加入以下内容:

LEFT JOIN hintout_thanks AS ht2 ON h.hintout_id = ht2.hintout_id

但是发生了奇怪的事情,我找不到任何答案或解决方案, 是在我添加这个附加部分的那一刻,cmets 的计数被破坏了(我得到了错误和奇怪的数字),并且我得到了相同的数字以表示感谢 - 我不明白为什么或如何解决它......我正在避免使用嵌套查询

所以任何帮助或指针将不胜感激!

ps:这可能已经发了两次了,但是我找不到之前的帖子了

【问题讨论】:

    标签: mysql join count


    【解决方案1】:

    添加时

    LEFT JOIN hintout_thanks AS ht2 ON h.hintout_id = ht2.hintout_id
    

    行数增加,您会得到表 hc 的重复行,这些行在 COUNT(hc.comment_id) 中被计算为双倍。 你可以替换

    COUNT(hc.comment_id) <<-- counts duplicated
    /*with*/ 
    COUNT(DISTINCT(hc.comment_id))  <<-- only counts unique ids
    

    仅计算 id 上的唯一外观。

    对于不唯一的值,例如 co.county_namecount(distinct 将不起作用,因为它只会列出不同的国家/地区(如果您的所有结果都在美国,则计数将为 1)。

    Quassnoi
    通过将计数放入子选择中解决了整个计数问题,以便所有这些连接引起的额外行不会影响这些计数。

    【讨论】:

    • 啊哈,我明白了,子选择是个好主意还是找到使用连接或不同的方法更好,或者我不知道还有什么其他方法...?
    • @roy naufal,两者都有自己的位置。我总是先选择join + distinct 路线,如果太慢,我会尝试子选择,看看是否更快。您需要对代码进行计时并对其进行测试,看看哪个更好。
    【解决方案2】:
    SELECT  h.*, h.permalink AS hintout_permalink, hi.hinter_name,
            hi.permalink, hf.user_id AS followed_hid,
            ci.city_id, ci.city_name, co.country_id, co.country_name,
            ht.thank_id,
            COALESCE(
            (
            SELECT  COUNT(*)
            FROM    hintout_comments hci
            WHERE   hc.hintout_id = h.hintout_id
            ), 0) AS commentsCount,
            COALESCE(
            (
            SELECT  COUNT(*)
            FROM    hintout_comments hti
            WHERE   hti.hintout_id = h.hintout_id
            ), 0) AS thanksCount
    FROM    hintouts AS h 
    JOIN    hinter_follows AS hf
    ON      hf.hinter_id = h.hinter_id
    JOIN    hinters AS hi
    ON      hi.hinter_id = h.hinter_id
    LEFT JOIN
            cities AS ci
    ON      ci.city_id = h.city_id
    LEFT JOIN
            countries as co
    ON      co.country_id = h.country_id
    LEFT JOIN
            hintout_thanks AS ht
    ON      ht.hintout_id = h.hintout_id
            AND ht.thanker_user_id=1 
    WHERE   hf.user_id = 1 
    

    【讨论】:

      猜你喜欢
      • 2015-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-25
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      相关资源
      最近更新 更多