数据库中差集的使用(Except/Minus/NOT IN/Left Join)
MINUS,比较两个查询的结果,返回在第一个查询结果集中,但不是第二个查询结果集中的行记录,也就是说不包含两个查询返回集合的相交部分。
请注意
某些数据库系统中,例如Microsoft SQLServer,PostgreSQL等,使用Except运算符来实现相同的功能。在Oracle中,使用MINUS运算符。在MySQL中不支持MINUS运算符,但是可以通过Left JOIN连接或者NOT IN运算符来实现
<!-- 查询当前用户所在单位的用户,当前用户所在单位具有项目负责人权限的用户,做差集 -->
(select t1.*
MySQL中的差集实现可以参考:http://www.yiibai.com/mysql/minus.html
from act_id_user t1
left join userInfo t3 on t1.id_ = t3.id_
where t3.deptid = #{deptID})
minus
(select t1.*
from act_id_user t1
left join act_id_membership t2 on t1.id_ = t2.user_id_
left join userInfo t3 on t1.id_ = t3.id_
where t2.group_id_ = 'G0004' and t3.deptid = #{deptID})