我从两张表中提取数据'逗留'和宠物'使用连接函数的表。我想知道我怎么能算出时间的数量' cat'发生在返回的数据中。
SELECT pet.petID, pet.species
from stay
inner join pet
on (stay.petID = pet.petID)
petID Species
8 Cat
8 Cat
8 Cat
9 Cat
11 Cat
12 Cat
12 Cat
14 Cat
39 Dog
39 Dog
40 Dog
41 Dog
答案 0 :(得分:1)
此查询应该可以解决问题:
SELECT COUNT(*)
FROM stay
INNER JOIN pet
ON stay.petID = pet.petID
WHERE pet.species = 'Cat'
但是更有趣(也可能更有用)的结果集会显示所有动物类型的计数,在这种情况下你可以试试这个:
SELECT pet.species,
COUNT(*) AS petCount
FROM stay
INNER JOIN pet
ON stay.petID = pet.petID
GROUP BY pet.species
答案 1 :(得分:0)
将pet.petID,count(pet.species)选为no_of_species,pet.species 从逗留 内心加入宠物 on(stay.petID = pet.petID) pet.species分组
答案 2 :(得分:0)
仅添加WHERE species = 'cat'
SELECT COUNT(*)
from stay
inner join pet
on (stay.petID = pet.petID)
where pet.species = 'Cat'