我想问一下我的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转换它们?
答案 0 :(得分:0)
以下是如何使用MySQL获取最小和最大聚合函数
SELECT student_name, MIN(test_score), MAX(test_score)
FROM student
GROUP BY student_name;
如果你想把它作为一个视图
CREATE VIEW vw_student_score AS
SELECT student_name, MIN(test_score), MAX(test_score)
FROM student
GROUP BY student_name;