I'm trying to use a count in my MySQL query to count the amount of rows with a matching column, and the query is working, however when the select query the count is being used in returns no rows, the count value仍然返回 0,因此查询将返回一个空行,但计数列除外,该列具有 0。
这是我的查询:
SELECT
forum_threads_forums.id,
forum_threads.title,
forum_threads.description,
forum_threads.views,
COUNT(DISTINCT forum_posts_threads.post_id) AS post_count,
users.id AS user_id,
users.username AS user_username
FROM
forum_threads
INNER JOIN
forum_threads_forums
ON
forum_threads.id=forum_threads_forums.thread_id
AND
forum_threads_forums.forum_id=:forum_id
LEFT OUTER JOIN
forum_posts_threads
ON
forum_posts_threads.thread_id=forum_threads_forums.thread_id
INNER JOIN
users
ON
forum_threads.user_id=users.id
When the select query doesn't return any rows, the count still does.所以我得到这样的结果:
id title description views post_count user_id user_username
0
我希望它不返回任何内容,这样我就可以用 PHP 可靠地计算结果。
有没有人知道如何实现这一点?我迷路了。
提前感谢您的帮助。
最佳答案
添加:
HAVING post_count > 0
到查询结束。
如果有任何匹配项,它将返回 1 行,post_count
将包含不同帖子的数量,如果没有匹配项,则返回 0 行。
关于php - 如果没有找到行,MySQL make count 不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26695376/