【发布时间】:2017-10-28 19:38:57
【问题描述】:
我需要一些帮助来创建 MySQL 查询。假设我们有一个“动物”表,其中包含物种以及动物的详细品种。此外,我们还有一个表格“考试”,其中包含对动物的调查。要了解检查了某些品种的动物有多少(我们对找出某些品种进行了多少检查不感兴趣!),我们可以运行以下查询:
SELECT animals.animal_species,
animals.animal_breed,
Count(DISTINCT animals.animal_id)
FROM examinations,
animals
WHERE examinations.examination_animal_id = animals.animal_id
AND animals.animal_breed IS NOT NULL
GROUP BY animals.animal_species,
animals.animal_breed
ORDER BY animals.animal_species
通过运行我们得到如下结果:
dog | sheepdog | 3
dog | collie | 1
dog | terrier | 5
cat | Persian cat | 3
cat | Birman cat | 2
现在我想包括每个物种的总和。结果应如下所示:
dog | sheepdog | 3 | 9
dog | collie | 1 | 9
dog | terrier | 5 | 9
cat | Persian cat | 3 | 5
cat | Birman cat | 2 | 5
您能告诉我如何更改查询以实现此目的吗?我尝试了几种解决方案,但都没有奏效......
非常感谢您!
【问题讨论】:
-
提供具体的总和?
标签: mysql select count subquery