【发布时间】: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:这可能已经发了两次了,但是我找不到之前的帖子了
【问题讨论】: