2017-02-15 24 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'); 

どのように私はこれを結合として書くことができますか?サブクエリを結合としてnullにする方法

+0

なぜでしょうか?しかし、とにかく、あなたは**反組合**を探しています。 –

+0

@Ramanilここには「JOIN」は必要ありません。あなたは1つのテーブルのデータだけを望んでいるのですが、なぜそれを他のテーブルに結合するのですか? –

+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の左結合です。

乾杯 -

マルコ・セイン

関連する問題