COUNT(*)有和没有GROUP BY,没有匹配的行

时间:2017-09-19 19:02:31

标签: mysql sql

考虑具有以下记录的关系表(Balance,Customer):

screenshot of database table

现在我尝试了these two queries here

-- Query 1:
select A.Customer, count(B.Customer)
from account A, account B
where A.balance < B.balance
group by A.Customer;

-- Query 2:
select A.Customer, count(B.Customer)
from account A, account B
where A.balance < B.balance;

第一个查询没有输出。使用第二个查询,我得到一个count = 0的输出。

在这两种情况下,没有满足where子句中的条件的行,因此不返回任何行。那么为什么count函数仅在第二种情况下给出输出?

2 个答案:

答案 0 :(得分:5)

没有group by 总是的聚合查询返回一行(如果它在语法上是正确的)。这一行中的计数为0

使用group by的聚合查询每组返回一行。如果没有组,则没有行。

答案 1 :(得分:0)

SQL不返回任何数据,因为您没有满足A.balance的条件的数据