2017-02-15 46 views
1
select EmpName, EmpCode 
from employees 
where EmpBrnID = 461 and EmpIsActive =1 
    and EmpCode not in (select EmpCode from reports 
         where BranchID = 461 and DAYOFWEEK(InTime)!= 1 
         and InTime BETWEEN '2017-01-31'- INTERVAL 6 DAY AND '2017-01-31'); 

我該怎麼寫這個連接。如何寫子查詢不爲空作爲連接

+0

爲什麼你會嗎?但無論如何,你正在尋找**反連接**。 –

+0

@Ramanil你不需要'JOIN'在這裏。你只需要一張表的數據,那麼你爲什麼要加入其他表呢? –

+0

子查詢需要更多時間才能得到結果,所以我想寫join.how以便快速獲得子查詢。@ Jibin Balachandran – Ramanil

回答

0
select EmpName, EmpCode 
from employees 
left join reports on reports.EmpCode = employees.EmpCode 
    and BranchID = 461 and DAYOFWEEK(InTime)!= 1 
         and InTime BETWEEN '2017-01-31'- INTERVAL 6 DAY AND '2017-01-31' 
where EmpBrnID = 461 and EmpIsActive =1 
    and reports.EmpCode IS NULL 
1

你的意思是這樣的嗎?

SELECT 
    empname 
, empcode 
FROM employees e 
LEFT JOIN reports r 
    ON e.empbrnid=r.branchid 
WHERE e.empbrnid=461 
    AND DAYOFWEEK(intime)!=1 
    AND InTime BETWEEN '2017-01-31'- INTERVAL 6 DAY AND '2017-01-31') 
    AND r.branchid IS NULL 
; 

所以這是一個左連接,右表的列爲NULL。

乾杯 -

馬爾科理智