如果我使用where where
,我的查询工作正常
select a.mnth,count(b.month) from table1 b right join (SELECT 'January' mnth,'01' as set_or
UNION ALL
SELECT 'February' mnth,'02' as set_or
UNION ALL
SELECT 'March' mnth,'03' as set_or
UNION ALL
SELECT 'APRIL' mnth,'04' as set_or
UNION ALL
SELECT 'MAY' mnth,'05' as set_or
UNION ALL
SELECT 'JUNE' mnth,'06' as set_or
UNION ALL
SELECT 'JULY' mnth,'07' as set_or
UNION ALL
SELECT 'AUGUST' mnth,'08' as set_or
UNION ALL
SELECT 'SEPTEMBER' mnth,'09' as set_or
UNION ALL
SELECT 'OCTOBER' mnth,'10' as set_or
UNION ALL
SELECT 'NOVEMBER' mnth,'11' as set_or
UNION ALL
SELECT 'DECEMBER' mnth,'12' as set_or
) a on a.mnth = b.month group by a.mnth order by a.set_or asc;
+-----------+----------------+
| mnth | count(b.month) |
+-----------+----------------+
| January | 0 |
| February | 0 |
| March | 2 |
| APRIL | 1 |
| MAY | 0 |
| JUNE | 1 |
| JULY | 1 |
| AUGUST | 0 |
| SEPTEMBER | 0 |
| OCTOBER | 0 |
| NOVEMBER | 0 |
| DECEMBER | 0 |
+-----------+----------------+
但我使用condion在同一个查询中它不显示0反对 每个月的名字
select a.mnth,count(b.month) from table1 b right join (SELECT 'January' mnth,'01' as set_or
UNION ALL
SELECT 'February' mnth,'02' as set_or
UNION ALL
SELECT 'March' mnth,'03' as set_or
UNION ALL
SELECT 'APRIL' mnth,'04' as set_or
UNION ALL
SELECT 'MAY' mnth,'05' as set_or
UNION ALL
SELECT 'JUNE' mnth,'06' as set_or
UNION ALL
SELECT 'JULY' mnth,'07' as set_or
UNION ALL
SELECT 'AUGUST' mnth,'08' as set_or
UNION ALL
SELECT 'SEPTEMBER' mnth,'09' as set_or
UNION ALL
SELECT 'OCTOBER' mnth,'10' as set_or
UNION ALL
SELECT 'NOVEMBER' mnth,'11' as set_or
UNION ALL
SELECT 'DECEMBER' mnth,'12' as set_or
) a on a.mnth = b.month where b.year like '2016' group by a.mnth order by a.set_or asc;
+-------+----------------+
| mnth | count(b.month) |
+-------+----------------+
| March | 2 |
| APRIL | 1 |
| JUNE | 1 |
| JULY | 1 |
+-------+----------------+
这是我的表格描述
表1;
+-------------+---------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+-------------------+-----------------------------+
| month | varchar(100) | NO | | NULL | |
| Year | int(100) | NO | | NULL | |
+-------------+---------------+------+-----+-------------------+------------------------------
任何人都可以请告诉我,我怎么能得到0计数是任何月份没有 存在?
答案 0 :(得分:2)
我已重新排列表格的顺序(代码中的外观),并分别将RIGHT JOIN
更改为LEFT JOIN
。将WHERE子句约束放入JOIN中,以便不暗示INNER JOIN
:
select
a.mnth,
count(b.month)
from (
SELECT 'January' mnth,'01' as set_or
UNION ALL
SELECT 'February' mnth,'02' as set_or
UNION ALL
SELECT 'March' mnth,'03' as set_or
UNION ALL
SELECT 'APRIL' mnth,'04' as set_or
UNION ALL
SELECT 'MAY' mnth,'05' as set_or
UNION ALL
SELECT 'JUNE' mnth,'06' as set_or
UNION ALL
SELECT 'JULY' mnth,'07' as set_or
UNION ALL
SELECT 'AUGUST' mnth,'08' as set_or
UNION ALL
SELECT 'SEPTEMBER' mnth,'09' as set_or
UNION ALL
SELECT 'OCTOBER' mnth,'10' as set_or
UNION ALL
SELECT 'NOVEMBER' mnth,'11' as set_or
UNION ALL
SELECT 'DECEMBER' mnth,'12' as set_or
) a
left join donation_entry b on
a.mnth = b.month
and b.year = '2016' -- here is the change
group by a.mnth
order by a.set_or asc;
请注意,我还用等号运算符like
替换了=
,因为没有用于部分匹配的通配符,它基本上是相同的,可以避免混淆。