0

我有 4 个表,如下所示

医生

id   name
------------
1   Mathew
2   Praveen
3   Rosie
4   Arjun
5   Denis

医生预约

id  doctors_id    patient_name    contact          date               status
--------------------------------------------------------------------------------------
1   5             Nidhin        9876543210  2012-12-10 15:39:41     Registered
2   5             Sunny         9876543210  2012-12-18 15:39:48     Registered
3   5             Mani          9876543210  2012-12-12 15:39:57     Registered
4   2             John          9876543210  2012-12-24 15:40:09     Registered
5   4             Raj           9876543210  2012-12-05 15:41:57     Registered
6   3             Samuel        9876543210  2012-12-14 15:41:33     Registered
7   2             Louis         9876543210  2012-12-24 15:40:23     Registered
8   1             Federick      9876543210  2012-12-28 15:41:05     Registered
9   2             Sam           9876543210  2012-12-12 15:40:38     Registered
10  4             Sita          9876543210  2012-12-12 15:41:00     Registered

医生_工作计划

id   doctor_id    weeks             time           no_of_patients
------------------------------------------------------------------
1       1         3,6,7         9:00am-1:00pm          10
2       2         3,4,5         1:00pm-4:00pm          7
3       3         3,6,7         10:00am-2:00pm         10
4       4         3,4,5,6       8:30am-12:30pm         12
5       5         3,4,5,6,7     9:00am-4:00pm          30

emp_leave

id    empid        leavedate
--------------------------------
1   2   2012-12-05 14:42:36
2   2   2012-12-03 14:42:59
3   3   2012-12-03 14:43:06
4   3   2012-12-06 14:43:14
5   5   2012-12-04 14:43:24

我的任务是找出一个月中除了休假日期外,医生有空的所有日子。

我的查询内容如下:

SELECT DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) AS Date,
ROW +1 AS DayOfMonth
FROM (

SELECT @row := @row +1 AS
ROW FROM (

SELECT 0
UNION ALL SELECT 1
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
)t1, (

SELECT 0
UNION ALL SELECT 1
UNION ALL SELECT 3
UNION ALL SELECT 4
UNION ALL SELECT 5
UNION ALL SELECT 6
)t2, (

SELECT @row := -1
)t3
LIMIT 31
)b
WHERE DATE_ADD( '2012-12-01', INTERVAL
ROW DAY )
BETWEEN '2012-12-01'
AND '2012-12-31'
AND DAYOFWEEK( DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) ) =2
AND DATE_ADD( '2012-12-01', INTERVAL
ROW DAY ) NOT
IN (

SELECT DATE_FORMAT( l.leavedate, '%Y-%m-%d' ) AS date
FROM doctors_dutyplan d
LEFT JOIN emp_leave AS l ON d.doctor_id = l.empid
WHERE doctor_id =2
)

这适用于所有在一个月内某一天休假的医生(在此示例中为 2012 年 12 月)。上述查询的结果如下所示:

Date         DayOfMonth
-----------------------
2012-12-10       10
2012-12-17       17
2012-12-24       24
2012-12-31       31

但另一方面,对于没有请假的医生,因为我的查询显示的是空表,例如对于 id 为 1 的医生 Mathew,我的查询返回一个空结果

谁能告诉这个问题的解决方案。

提前致谢。

4

1 回答 1

2

您的查询很大,但这部分看起来很可疑:

NOT IN (
    SELECT DATE_FORMAT( l.leavedate, '%Y-%m-%d' ) AS date
    FROM doctors_dutyplan d
    LEFT JOIN emp_leave AS l ON d.doctor_id = l.empid
    WHERE doctor_id =2

将为医生 1 返回a的left join平均值。现在,它的行为与您预期的不同。它转化为:nullcol1 not in (null)

col1 <> null

这从来都不是真的。您可以通过将 更改为left join来解决此问题,因此无需请假就为医生返回inner join一个空集而不是。null

于 2012-12-08T11:12:41.707 回答