那是因为你使用GROUP BY
. 这意味着您可以获得i.items
表中每个不同的计数。
您可以通过将该项目添加到查询结果中来检查(我更改WHERE
为ON
):
SELECT
i.items,
COUNT(*) AS comment
FROM
comment co
INNER JOIN item i ON co.items = i.items
GROUP BY
i.items
解决方案是指定您想要的项目....
SELECT
i.items,
COUNT(*) AS comment
FROM
comment co
INNER JOIN item i ON co.items = i.items
WHERE
i.items = 'foo'
/* -- Grouping not needed anymore, since you only have 1 item (= 1 group)
GROUP BY
i.items*/
...或不分组,因此您可以获得所有项目的总数:
-- Will fetch the total number of comments (that are linked to an item)
SELECT
COUNT(*) AS comment
FROM
comment co
INNER JOIN item i ON co.items = i.items
... 或限制结果 此查询仅截断结果,仅返回其中一项的计数。该项目现在或多或少是随机选择的,尽管您可以添加 ORDER BY 来影响它。
SELECT
COUNT(*) AS comment
FROM
comment co
INNER JOIN item i ON co.items = i.items
GROUP BY
i.items
/* -- Optional order, if you need to influence which of the items is preffered.
ORDER BY
i.items*/
LIMIT 1
因此,从您的问题中,我只能说出问题所在,而不是哪种解决方案最适合您。但这里有几个选项可供选择。:)