php - 如果没有找到行,MySQL make count 不返回任何内容

标签 php mysql pdo

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/

相关文章:

mysql group by、max、count实现

php - Ajax获取消息问题

php - 在 PHP 不工作的情况下向浏览器显示 PDF

mysql - 根据一列对多个表进行排序

mysql group 组的行

pdo - MAMP Pro 和 PDO-dblib

javascript - 尝试使用 AJAX 提交而不刷新,失败无提示

php - 使用 PDO 将数组的日期格式更改为 MySQL 格式

php - 警告:PDOStatement::execute():SQLSTATE[HY093]:参数数量无效:绑定(bind)变量的数量与标记数量不匹配

javascript - 如何管理mysql where子句中 "+"的数据