Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mysql中使用join函数计算特定数据出现的次数_Mysql - Fatal编程技术网

在mysql中使用join函数计算特定数据出现的次数

在mysql中使用join函数计算特定数据出现的次数,mysql,Mysql,我使用连接函数从两个表“stay”和“pet”中获取数据。我想知道如何计算返回数据中出现“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

我使用连接函数从两个表“stay”和“pet”中获取数据。我想知道如何计算返回数据中出现“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

此查询应实现以下功能:

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
选择pet.petID,将(pet.species)计数为物种中的无物种,pet.species 从停留 内连接pet 打开(stay.petID=pet.petID)
按宠物种类分组

只添加
,其中种类='cat'

SELECT COUNT(*)
from stay
inner join pet
    on (stay.petID = pet.petID)
where pet.species = 'Cat'

如果在查询的末尾添加
WHERE species='cat'
,那么答案将与返回的行数相同