我想问一下我的 sql 以从视图中找到最大值和最小值。
我创建了一个视图:
create VIEW Count_Of_Appointment AS
SELECT ap.doctorID, COUNT(*) AS NumberOfAppointment, CONCAT(d.surname,'', d.given) AS doctor_name
FROM appointment AS ap Inner join doctor AS d
ON ap.doctorid = d.doctorID
GROUP BY doctorID
ORDER BY COUNT(*)
现在,我想从使用该视图中找到 min + 医生姓名。
这是我的代码:
SELECT a2.*
FROM
(
SELECT ap.doctorID, COUNT(*) AS Number, CONCAT(d.surname,'', d.given) AS doctor_name
FROM appointment AS ap Inner join doctor AS d
ON ap.doctorid = d.doctorID
GROUP BY doctorID
ORDER BY COUNT(*)
)a2
INNER JOIN
(
SELECT MIN(Number) AS NumberOfAppointment
FROM
(
SELECT ap.doctorID, COUNT(*) AS Number, CONCAT(d.surname,'', d.given) AS doctor_name
FROM appointment AS ap Inner join doctor AS d
ON ap.doctorid = d.doctorID
GROUP BY doctorID
ORDER BY COUNT(*)
)a1
)b
ON a2.Number = b.NumberOfAppointment
我在使用视图之前正在执行该代码。如何使用 FROM VIEW Count_Of_Appointment 转换它们?