我有表[Surgery_By]
表[Surgery]
表[Doctor]
我正在使用ASP.NET和SQL Server:
表[Surgery_By]
包含以下列:
1-ID (PK)
2-Surgery ID (FK)
3-Doctor ID (FK)
如何按手术次数显示医生?
答案 0 :(得分:0)
以这种方式试试
SELECT d.id, d.fullname, COUNT(s.id) total_surgeries
FROM doctor d LEFT JOIN surgery_by s
ON d.id = s.doctor_id
GROUP BY d.id, d.fullname
ORDER BY total_surgeries DESC
示例输出:
| ID | FULLNAME | TOTAL_SURGERIES | |----|------------|-----------------| | 1 | John Doe | 3 | | 2 | Jane Doe | 1 | | 3 | Mark Smith | 0 |
这是 SQLFiddle 演示
答案 1 :(得分:0)
这是在黑暗中刺伤。
Select Doctor.ID As DoctorID
,Count(*) As Count
From Doctor
Join Surgery_By
On Doctor.ID = Surgery_By.DoctorID
Group By Doctor.DoctorID
Order By Count(*)
我不确定您是否希望合并表Surgery
(但如果您这样做,则加入将非常简单 - 只需确保将所选列添加到Group By
语句中。)< / p>
从ASP.NET,您可以select this data from a SQL Command。