现有表格Department(科室),DoctorLevel(医生级别),Doctor(医生),Room(诊室),Schedule(排班),要求用mysql实现下列语句: 1、查询医生“陈斌”值班的日期、时段、诊室名称、诊室位置。 2、查询级别名称为 “二级专家”的医生姓名、诊疗费用、科室名称。 3、查询在2022年4月18一天中上午、下午均值班的医生的工作证号。 4、查询在2022年4月18和4月19日两天均未值班的医生的工作证号、姓名、科室名称。 5、查询每个科室的科室编码及医生人数(要求按科室编码分组)。 6、查询科室级别为”2”的每个科室的科室编码及医生人数(要求按科室编码分组) 7、查询各个医生级别的级别编码、级别名称及其人数。(要求只按医生级别分组,统计人数建议用派生表) 8、查询每个医生的工作证号、姓名和值班次数。(半天算值班1次,要求只按工作证号分组,统计次数建议用派生表实现)
时间: 2023-12-16 21:05:11 浏览: 200
将doctor的信息匹配一级科室。使用笛卡尔积匹配。 结果写入mysql。
1、查询医生“陈斌”值班的日期、时段、诊室名称、诊室位置:
```
SELECT Schedule.Date, Schedule.Time, Room.Name, Room.Location
FROM Doctor
INNER JOIN Schedule ON Doctor.DoctorID = Schedule.DoctorID
INNER JOIN Room ON Schedule.RoomID = Room.RoomID
WHERE Doctor.Name = '陈斌';
```
2、查询级别名称为“二级专家”的医生姓名、诊疗费用、科室名称:
```
SELECT Doctor.Name, DoctorLevel.Fee, Department.Name
FROM Doctor
INNER JOIN DoctorLevel ON Doctor.LevelID = DoctorLevel.LevelID
INNER JOIN Department ON Doctor.DepartmentID = Department.DepartmentID
WHERE DoctorLevel.Name = '二级专家';
```
3、查询在2022年4月18一天中上午、下午均值班的医生的工作证号:
```
SELECT Schedule.DoctorID
FROM Schedule
WHERE Schedule.Date = '2022-04-18'
GROUP BY Schedule.DoctorID
HAVING COUNT(DISTINCT Schedule.Time) = 2;
```
4、查询在2022年4月18和4月19日两天均未值班的医生的工作证号、姓名、科室名称:
```
SELECT Doctor.DoctorID, Doctor.Name, Department.Name
FROM Doctor
INNER JOIN Department ON Doctor.DepartmentID = Department.DepartmentID
WHERE Doctor.DoctorID NOT IN (
SELECT DISTINCT Schedule.DoctorID
FROM Schedule
WHERE Schedule.Date IN ('2022-04-18', '2022-04-19')
)
```
5、查询每个科室的科室编码及医生人数(要求按科室编码分组):
```
SELECT Department.DepartmentID, COUNT(Doctor.DoctorID)
FROM Doctor
INNER JOIN Department ON Doctor.DepartmentID = Department.DepartmentID
GROUP BY Department.DepartmentID;
```
6、查询科室级别为”2”的每个科室的科室编码及医生人数(要求按科室编码分组):
```
SELECT Department.DepartmentID, COUNT(Doctor.DoctorID)
FROM Doctor
INNER JOIN Department ON Doctor.DepartmentID = Department.DepartmentID
WHERE Doctor.LevelID = 2
GROUP BY Department.DepartmentID;
```
7、查询各个医生级别的级别编码、级别名称及其人数。(要求只按医生级别分组,统计人数建议用派生表):
```
SELECT DoctorLevel.LevelID, DoctorLevel.Name, COUNT(Doctor.DoctorID) AS NumOfDoctors
FROM Doctor
INNER JOIN DoctorLevel ON Doctor.LevelID = DoctorLevel.LevelID
GROUP BY DoctorLevel.LevelID;
```
8、查询每个医生的工作证号、姓名和值班次数。(半天算值班1次,要求只按工作证号分组,统计次数建议用派生表实现):
```
SELECT Doctor.DoctorID, Doctor.Name, COUNT(DISTINCT CONCAT(Schedule.Date, Schedule.Time)) AS NumOfDuty
FROM Doctor
INNER JOIN Schedule ON Doctor.DoctorID = Schedule.DoctorID
GROUP BY Doctor.DoctorID;
```
阅读全文