我对MySQL查询有点困惑,需要一些帮助。
我们假设我们有一张桌子"动物"使用这些列:
animal_id | animal_species
1 | dog
2 | cat
3 | dog
4 | pig
此外,我们有一张表"考试"其中包括对这些动物的各种检查:
examination_id | examination_animal_id
1 | 1
2 | 1
3 | 3
4 | 2
5 | 4
最后我们有另一张桌子" diagnoses_statistics"对于某些检查包含一个或多个诊断:
diagnoses_statistics_id | diagnoses_statistics_examination_id | diagnoses_statistics_diagnosis
1 | 1 | luxation
2 | 3 | thrombopathy
3 | 3 | trauma
4 | 3 | luxation
5 | 5 | trauma
所有表格都包含许多其他列,我只是缩短了它们,因为上面的示例应足以说明我的问题。
我们想知道每个物种有多少相同的诊断。为此,我们可以运行此查询:
SELECT animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis,
COUNT(diagnoses_statistics.diagnoses_statistics_diagnosis) AS diagnoses_count
FROM examinations
JOIN animals ON animals.animal_id = examinations.examination_animal_id
JOIN diagnoses_statistics ON diagnoses_statistics.diagnoses_statistics_examination_id = examinations.examination_id
GROUP BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
ORDER BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
这给了我们这样的东西:
animal_species | diagnoses_statistics_diagnosis | diagnoses_count
dog | luxation | 2
dog | thrombopathy | 1
dog | trauma | 1
pig | trauma | 1
到目前为止,这么好。现在我们想要添加一个列,其中包含任何包含任何诊断的物种的总和:
animal_species | diagnoses_statistics_diagnosis | diagnoses_count | species_count
dog | luxation | 2 | 2
dog | thrombopathy | 1 | 2
dog | trauma | 1 | 2
pig | trauma | 1 | 1
为了达到这个目的,我尝试了这个代码,它似乎在" species_count"中产生了正确的值,但它打破了列" diagnoses_count":
SELECT animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis,
COUNT(diagnoses_statistics.diagnoses_statistics_diagnosis) AS diagnoses_count,
species_count.species_count
FROM examinations
JOIN animals ON animals.animal_id = examinations.examination_animal_id
JOIN diagnoses_statistics ON diagnoses_statistics.diagnoses_statistics_examination_id = examinations.examination_id
JOIN
(SELECT animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis,
COUNT(animals.animal_id) AS species_count
FROM examinations
JOIN animals ON animals.animal_id = examinations.examination_animal_id
JOIN diagnoses_statistics ON diagnoses_statistics.diagnoses_statistics_examination_id = examinations.examination_id
GROUP BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
) AS species_count ON species_count.animal_species = animals.animal_species
GROUP BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
ORDER BY animals.animal_species,
diagnoses_statistics.diagnoses_statistics_diagnosis
也许我这样做完全错了。
你能帮我解决一下这个问题吗?
非常感谢您的帮助。
答案 0 :(得分:1)
你非常接近。我想这就是你的意思。请注意子查询中的COUNT(DISTINCT ...)
:
SELECT
a.animal_species,
d.diagnoses_statistics_diagnosis,
COUNT(1) as diagnoses_count,
spc.species_count
FROM
animals a
JOIN examinations e ON
e.examination_animal_id = a.animal_id
JOIN diagnoses_statistics d ON
d.diagnoses_statistics_examination_id = e.examination_id
JOIN (SELECT
a2.animal_species,
COUNT(DISTINCT a2.animal_id) as species_count
FROM
animals a2
JOIN examinations e2 ON
e2.examination_animal_id = a2.animal_id
JOIN diagnoses_statistics d2 ON
d2.diagnoses_statistics_examination_id = e2.examination_id
GROUP BY
a2.animal_species
) spc ON
spc.animal_species = a.animal_species
GROUP BY
a.animal_species,
d.diagnoses_statistics_diagnosis,
spc.species_count
您可以在rextester.com/XXQ94494处尝试一下。 @p.Salmon的帽子提示,用于构建数据集的代码。
答案 1 :(得分:0)
我不知道如何获得狗的物种数量2,因为这个物种有3个诊断。如果3是正确的,您可以使用子查询。
DROP TABLE IF EXISTS ANIMALS;
CREATE TABLE ANIMALS (animal_id INT, animal_species VARCHAR(3));
INSERT INTO ANIMALS VALUES
(1 , 'dog'),
(2 , 'cat'),
(3 , 'dog'),
(4 , 'pig');
DROP TABLE IF EXISTS EXAMINATIONS;
CREATE TABLE EXAMINATIONS (examination_id INT, examination_animal_id INT);
INSERT INTO EXAMINATIONS VALUES
(1 , 1),
(2 , 1),
(3 , 3),
(4 , 2),
(5 , 4);
DROP TABLE IF EXISTS diagnoses_statistics;
CREATE TABLE diagnoses_statistics (diagnoses_statistics_id INT, diagnoses_statistics_examination_id INT, diagnoses_statistics_diagnosis VARCHAR(20));
INSERT INTO diagnoses_statistics VALUES
(1 , 1 , 'luxation'),
(2 , 3 , 'thrombopathy'),
(3 , 3 , 'trauma'),
(4 , 3 , 'luxation'),
(5 , 5 , 'trauma');
SELECT A.ANIMAL_SPECIES, D.diagnoses_statistics_diagnosis, COUNT(*) DIAGNOSES_COUNT
,(SELECT COUNT(DISTINCT diagnoses_statistics_diagnosis)
FROM ANIMALS A1
JOIN EXAMINATIONS E ON E.EXAMINATION_ANIMAL_ID = A1.ANIMAL_ID
JOIN diagnoses_statistics D ON D.diagnoses_statistics_EXAMINATION_ID = E.EXAMINATION_ID
WHERE A1.ANIMAL_SPECIES = A.ANIMAL_SPECIES) SPECIES_COUNT
FROM ANIMALS A
JOIN EXAMINATIONS E ON E.EXAMINATION_ANIMAL_ID = A.ANIMAL_ID
JOIN diagnoses_statistics D ON D.diagnoses_statistics_EXAMINATION_ID = E.EXAMINATION_ID
GROUP BY A.ANIMAL_SPECIES, D.diagnoses_statistics_diagnosis
ORDER BY a.animal_species,
d.diagnoses_statistics_diagnosis
结果
+----------------+--------------------------------+-----------------+---------------+
| ANIMAL_SPECIES | diagnoses_statistics_diagnosis | DIAGNOSES_COUNT | SPECIES_COUNT |
+----------------+--------------------------------+-----------------+---------------+
| dog | luxation | 2 | 3 |
| dog | thrombopathy | 1 | 3 |
| dog | trauma | 1 | 3 |
| pig | trauma | 1 | 1 |
+----------------+--------------------------------+-----------------+---------------+
4 rows in set (0.00 sec)