我目前正在尝试列出每位兽医参加的访问次数。我必须将兽医的姓名详细信息与参加的访问次数一起包括在内。然后,我必须通过兽医标题对报告进行排序。
访问表格结构:
visit_id, vet_id, pet_id, Visit_Date, Basic_Cost, Symptom, Treatment
兽医表结构:
vet_id, Surname, Forename, Title, Ext_Number, Position, Salary
下面是我创建的初始SQL命令。它不起作用,我想知道如何为上述问题创建一个SQL命令。感谢所有帮助,请注意我使用ms访问。
SELECT vet_id, Title, Forename, Surname, COUNT(vet_id)
FROM visit, vet
ORDER BY Title;
答案 0 :(得分:2)
假设你想要所有的兽医,即使他们没有访问过......
SELECT P.vet_id, Title, Forename, Surname, coalesce(COUNT(V.vet_id),0) as VetVisitCount
FROM VET P
LEFT JOIN Visit V
on P.Vet_ID = V.Vet_ID
GROUP BY P.vet_id, Title, Forename, Surname
ORDER BY Title;
答案 1 :(得分:1)
SELECT vet_id, title, Forename, Surname,
(SELECT COUNT(visit.vet_id) FROM visit WHERE visit.vet_id = vet.vet_id) AS visits
FROM vet
ORDER BY vet.vet_id;
答案 2 :(得分:-1)
您需要GROUP BY
SELECT vet_id, Title, Forename, Surname, COUNT(vet_id)
FROM visit, vet
GROUP BY vet_id, Title, Forename, Surname
ORDER BY Title;