2013-01-19 98 views
0

我有兩個表疑慮查詢條件

1.Employee

EMP_NAME, 

EMP_CODE 

2.Vacations

EMP_NAME, 

EMP_CODE, 

VACATION_START_DATE-->date type 

VACATION_END_DATE-->date type, 

我的問題是如何查詢得到EMP_NAME從表1(員工),其中今天不在VACATION_START_DATE和之間from table2(Vacations)..

+1

emp名稱和代碼如何關聯表格eg如果emp_code在兩個emp_name是相同的? – Mark

+0

@Mark我發佈了答案,假設它是一個錯字... :) – bonCodigo

回答

0

請嘗試這個:假設您沒有emp_name休假表..以及emp_code作爲兩個表之間的關係。所以你可以使用join s。

SQLFIDDLE DEMO

select e.emp_code, e.emp_name, 
v.start_date, v.end_Date 
from emp e 
inner join 
vacation v 
on e.emp_code = v.emp_code 
where not (Now() between v.start_date 
and v.end_date) 
; 

| EMP_CODE | EMP_NAME |      START_DATE |      END_DATE | 
------------------------------------------------------------------------------------------- 
|  1 |  john | December, 10 2012 00:00:00+0000 | December, 20 2012 00:00:00+0000 | 
|  2 |  kate | December, 20 2012 00:00:00+0000 | December, 30 2012 00:00:00+0000 | 
|  3 |  tim | December, 24 2012 00:00:00+0000 | January, 01 2013 00:00:00+0000 | 
|  1 |  john | January, 01 2013 00:00:00+0000 | January, 08 2013 00:00:00+0000 | 
0

不知道你是否意味着「今天」是一個變量或系統日期,所以下面的代碼可能需要修改,但我認爲它可能工作:

SELECT 
    EMP_NAME 
FROM 
    EMPLOYEE 
WHERE 
    NOT EXISTS (SELECT * FROM VACATIONS WHERE EMP_NAME = EMPLOYEE.EMP_NAME AND 
       VACATIONS.VACATION_START_DATE >= Today AND 
       VACATIONS.VACATION_END_DATE <= Today)