没有为我的Mysql查询获得正确的预期输出?

时间:2012-12-08 11:02:55

标签: mysql sql select join union

我有4张桌子,如下所示

医生

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

doctors_appointments

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

doctors_dutyplan

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
)

这适用于所有在一个月中某一天休假的医生(这里的例子是Decemeber 2012)。以上查询的结果如下所示:

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

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

任何人都可以告诉这个问题的解决方案。

提前致谢。

1 个答案:

答案 0 :(得分: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

left join表示将为医生1返回null。现在,col1 not in (null)的行为与您的预期不符。它转换为:

col1 <> null

永远不会这样。您可以通过将left join更改为inner join来解决此问题,因此对于没有请假的医生,将返回空集而不是null