简体   繁体   English

MySQL计数查询与组不返回任何内容

[英]MySQL Count query with Group By not returning anything

I am trying to understand why this query in MySQL does not return any result at all whereas I think it should be returning one row with 0 as the column value 我试图理解为什么MySQL中的这个查询根本不返回任何结果,而我认为它应该返回一行,其中0作为列值

select count(ps.id) as totalcount 
from tblpresi ps
inner join users_staging.tbluser u on u.id = ps.userid
where ps.id = 3678
group by ps.id

The same query without the "Group By" works as expected. 没有“分组依据”的相同查询按预期工作。

Any ideas? 有任何想法吗?

No, it won't return that, since your query does not returns results at all (probably there is no ps.id = 3678, or your inner join rules out every row in the result set). 不,它不会返回,因为您的查询根本不返回结果(可能没有ps.id = 3678,或者您的内部联接排除了结果集中的每一行)。

If you change your query to: 如果您将查询更改为:

select count(1) as totalcount 
  from tblpresi ps
 inner join users_staging.tbluser u on u.id = ps.userid
 where ps.id = 3678
 group by ps.id

You will get one row with 0 . 你会得到一行0

Do keep in mind that if your ps.id is unique in the result set (I don't know if that inner join you have will return multiple rows with the same ps.id ) your group by clause is not necessary. 请记住, 如果您的ps.id在结果集中是唯一的(我不知道您所拥有的inner join ps.id是否将返回具有相同ps.id多个行),则ps.id group by子句。

Since you say you expect 1 row with zero count, I assume there is no ps.id of 3678. 既然你说你期望1行零计数,我假设没有ps.id为3678。

If you group-by on an empty result-set, there can be no groups. 如果在空结果集上进行分组,则不能有任何组。 The group by doesn't "know" about the where-clause, it's only looking through the result-set. group by不“知道”where子句,它只查看结果集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM