Heim  >  Artikel  >  Datenbank  >  NOT IN使用注意事项

NOT IN使用注意事项

WBOY
WBOYOriginal
2016-06-07 14:55:021345Durchsuche

无详细内容 无 下午被一条SQL折磨了select EMP_ID,EMP_NUM,NAME from employee WHERE emp_id in ( select distinct E.emp_id from EMPLOYEE E INNER JOIN PROJECT_EMPLOYEE pe on e.emp_id = pe.emp_id INNER JOIN PROJECT p on pe.pro_id = p.pro_id where p

下午被一条SQL折磨了
select EMP_ID,EMP_NUM,NAME
       from employee 
       WHERE 
       emp_id in
      (
       select  distinct E.emp_id
    from EMPLOYEE E 
    INNER JOIN PROJECT_EMPLOYEE pe on e.emp_id = pe.emp_id
    INNER JOIN PROJECT p on pe.pro_id = p.pro_id
    where pe.pro_id ='B49CBE19481447A68445461E7BE02B13'
 );//3条数据
因为业务上有变更,需要在此SQL基础上加上一些其他限制,修改后的SQL:
select EMP_ID,EMP_NUM,NAME
       from employee 
       WHERE 
       emp_id in
      (
       select  distinct E.emp_id
    from EMPLOYEE E 
    INNER JOIN PROJECT_EMPLOYEE pe on e.emp_id = pe.emp_id
    INNER JOIN PROJECT p on pe.pro_id = p.pro_id
    where pe.pro_id ='B49CBE19481447A68445461E7BE02B13'
    and e.emp_id not  in (select s.emp_id from salesteam s )
   );//0条数据,把not in 改成in有1条数据
纠结很久,发现是因为not in 后面的查询条件select s.emp_id from salesteam s所返回的数据中存在null的原因

解决办法就是将not in的子查询中加上is not null的条件
select s.emp_id from salesteam s where s.emp_id is not null
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:mysql导出csvNächster Artikel:MySQL 获取数据库实际的大小