Mysql将计数返回为0而不是零

Mysql将计数返回为0而不是零,mysql,Mysql,我在stack看到了其他问题,但我无法解决我的特定问题,下面是示例表 status area a1 b1 a2 b1 a1 b2 下面是我使用的查询 select count(*) as count, area, status from table group by area,status 这让我 1 b1 a1 1 b1 a2 1 b2 a1 我想要 0 b2 a2 还将显示 我知道我必须使用连接来执行此操作,因此我尝试了以下方法 select Distinc

我在stack看到了其他问题,但我无法解决我的特定问题,下面是示例表

status area
a1     b1
a2     b1
a1     b2
下面是我使用的查询

select count(*) as count, area, status from table group by area,status
这让我

1 b1 a1
1 b1 a2
1 b2 a1
我想要

0 b2 a2 
还将显示

我知道我必须使用连接来执行此操作,因此我尝试了以下方法

select Distinct(t1.status) as ds,t2.* from table as t1
right join
(select count(*), area, status from table group by area,status) t2
on t2.status=t1.status; 
但这只是给了我相同的结果,没有0

我也试过了,但显然有一个语法错误我搞不清楚

select t1.count,t2.ds,t1.area from
(select count(*) as count, area, status as ws from table group by area,status) t1
left join select distinct(status) as ds from table as t2
on t1.ws=t2.ds
尝试下面的查询

SELECT COUNT(t.area) count, c_t.area, c_t.status 
FROM
    (SELECT * FROM (SELECT DISTINCT status
      FROM table) t_status
      CROSS JOIN (SELECT DISTINCT area
        FROM table) t_area
     ) c_t
    LEFT JOIN table t 
     ON t.area = c_t.area
      AND t.status = c_t.status
GROUP BY c_t.area, c_t.status;

希望这对您有所帮助。

函数返回符合指定条件的行数。这意味着它的最小结果定义为零,即
COUNT()
不能返回“nothing”。

正如Tim在评论中指出的那样:您需要以某种方式引入丢失的记录,mysql无法自己找到它

您可以使用自联接在状态值和面积值之间创建笛卡尔联接,然后在此基础上再次左键联接原始表,并执行分组方式和计数:

select t1.status, t2.area, count(t3.status)
from (select distinct status from yourtable) t1
     join (select distinct area from yourtable) t2
     left join yourtable t3 on t3.status=t1.status and t3.area=t2.area
group by t1.status, t2.area

如果希望显示
0 b2 a2
,则需要一个表来介绍此信息,因为原始源表中缺少此信息。另外,请注意DISTINCT不是一个函数