我在计划应用程序中有两个SQL查询,以帮助检查员工的可用性。我需要执行左外连接,但我似乎无法使语法正确。
第一个查询获取在给定时间可用的员工列表,并具有执行要安排的任务的正确资格。
SELECT e.e_id FROM AppointmentTypes a1
INNER JOIN EmployeeQualifications eq ON a1.type_id = eq.ea_id
INNER JOIN Employees e ON e.e_id = eq.e_id
INNER JOIN EmployeeAvaliable ea ON ea.e_id = e.e_id
INNER JOIN EmployeeTimes et ON et.time_id = ea.time_id
INNER JOIN AppointmentTimes a2
INNER JOIN TypeTimes tt ON tt.time_id = a2.time_id AND tt.type_id = a1.type_id
WHERE et.wednesday = '1'
AND a1.type_id = '4'
AND et.start <= '10:00:00'
AND et.end > '10:00:00'
第二个查询获取已在此时安排了事件的员工列表。
SELECT e.e_id
FROM AppointmentTypes atp
INNER JOIN Appointments a ON atp.type_id = a.type_id
INNER JOIN EmployeesAttending ea ON a.a_id = ea.a_id
INNER JOIN Employees e ON e.e_id = ea.e_id
WHERE date(a.start_time) = '2014-12-10'
AND ADDTIME(time(a.start_time),'02:00:00') > '10:00:00'
AND time(a.start_time) < '12:00:00'
左外连接这些应该给我一份合格,可用且当时没有安排活动的员工名单。
答案 0 :(得分:0)
没有左外连接会给你左边的所有东西以及右边的可以连接的东西。在他们无法做到的地方。
我认为你要做的是让第一组中的所有内容都不在第二组中。
SELECT e.e_id FROM AppointmentTypes a1
INNER JOIN EmployeeQualifications eq ON a1.type_id = eq.ea_id
INNER JOIN Employees e ON e.e_id = eq.e_id
INNER JOIN EmployeeAvaliable ea ON ea.e_id = e.e_id
INNER JOIN EmployeeTimes et ON et.time_id = ea.time_id
INNER JOIN AppointmentTimes a2
INNER JOIN TypeTimes tt ON tt.time_id = a2.time_id AND tt.type_id = a1.type_id
WHERE et.wednesday = '1'
AND a1.type_id = '4'
AND et.start <= '10:00:00'
AND et.end > '10:00:00'
AND e.e_id NOT IN (
SELECT e.e_id
FROM AppointmentTypes atp
INNER JOIN Appointments a ON atp.type_id = a.type_id
INNER JOIN EmployeesAttending ea ON a.a_id = ea.a_id
INNER JOIN Employees e ON e.e_id = ea.e_id
WHERE date(a.start_time) = '2014-12-10'
AND ADDTIME(time(a.start_time),'02:00:00') > '10:00:00'
AND time(a.start_time) < '12:00:00'
)
答案 1 :(得分:0)
试试这个:
SELECT e.e_id
FROM AppointmentTypes a1
INNER JOIN EmployeeQualifications eq ON a1.type_id = eq.ea_id
INNER JOIN Employees e ON e.e_id = eq.e_id
INNER JOIN EmployeeAvaliable ea ON ea.e_id = e.e_id
INNER JOIN EmployeeTimes et ON et.time_id = ea.time_id
INNER JOIN AppointmentTimes a2
INNER JOIN TypeTimes tt ON tt.time_id = a2.time_id AND tt.type_id = a1.type_id
LEFT OUTER JOIN (SELECT DISTINCT e.e_id
FROM AppointmentTypes atp
INNER JOIN Appointments a ON atp.type_id = a.type_id
INNER JOIN EmployeesAttending ea ON a.a_id = ea.a_id
INNER JOIN Employees e ON e.e_id = ea.e_id
WHERE DATE(a.start_time) = '2014-12-10' AND
ADDTIME(TIME(a.start_time),'02:00:00') > '10:00:00' AND
TIME(a.start_time) < '12:00:00'
) AS A ON e.e_id = A.e.e_id
WHERE et.wednesday = '1' AND a1.type_id = '4' AND
et.start <= '10:00:00' AND et.end > '10:00:00' AND
A.e.e_id IS NULL;