Sql count包括没有值的项

时间:2016-03-15 05:23:14

标签: mysql sql

每个库都有2个不同的数据库。以下是样本数据

Table Store
Book Inventory
AAA x
AAA x
AAA x
AAA x
AAB x
AAB x
AAC x

Book Inventory
AAA x
AAA x
AAA x
AAB x
AAD x
AAE x


SELECT Book, COUNT(book) from Store where book in ('AAA','AAB','AAC','AAD','AAE') group by Book

结果跳过那些0值的书。即使计数为0,我如何得到返回结果?

Expected result
AAA 4
AAB 2
AAC 1
AAD 0
AAE 0

AAA 3
AAB 1
AAC 0
AAD 1
AAE 1

1 个答案:

答案 0 :(得分:0)

试试这个:

SELECT Book, SUM(book in ('AAA','AAB','AAC','AAD','AAE') ) AS cnt
from Store 
group by Book