mysql group by show count 0不确定我是否可以离开这里加入

时间:2015-07-16 22:54:23

标签: mysql group-by

我有这个查询,我想在计数为0时返回一行

SELECT count(day) as count, day
FROM `table`
GROUP BY `day`

其他stackoverflow答案说我需要进行左连接 但是我不知道如何在这种情况下这样做。

1 个答案:

答案 0 :(得分:1)

create table theDays
(   aDay varchar(20) not null
);

create table sales
(   id int auto_increment primary key,
    aDay varchar(20) not null,
    prodId int not null,
    qty int not null
);

insert theDays values ('sunday'),('monday'),('tuesday'),('wednesday'),('thursday'),('friday'),('saturday');
insert sales(aDay,prodId,qty) values ('tuesday',101,4),('thursday',107,2);

select d.aDay
from theDays d
left outer join sales s
on d.aDay=s.aDay
where s.aDay is null

+-----------+
| aDay      |
+-----------+
| sunday    |
| monday    |
| wednesday |
| friday    |
| saturday  |
+-----------+