如何编写子查询不为null作为连接

时间:2017-02-15 09:53:14

标签: mysql sql

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');

我怎么能把它写成连接。

2 个答案:

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

干杯 -

Marco the Sane

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