case when 语句


<if test="blackOwners != null and blackOwners.size >0 ">
	AND ( CASE WHEN o.orderType = 20 AND o.ownerPartyId not in 
   <foreach item="item" index="index" collection="blackOwners" open="(" separator="," close=")">
      #{item}
   </foreach> THEN 0 
   WHEN o.orderType != 20 THEN 0 
   ELSE 1 END ) = 0 
</if>

select a.orderCloseId, a.orderCode, a.status, a.remark, o.orderType, a.operLog, a.ownerPartyId, a.owner
from orderCloseApply a inner join order o on a.orderCode = o.orderCode
查出来58671条数据

select a.orderCloseApplyId, a.orderCode, a.status, a.remark, o.orderType, a.operLog, a.ownerPartyId, a.owner
from orderCloseApply a inner join TruckBrokerOrder o on a.orderCode = o.orderCode
and (CASE WHEN o.orderType = 20 AND o.ownerPartyId not in (568269285) THEN 0  
          WHEN o.orderType != 20 THEN 0
          ELSE 1 
          END ) = 0 
查出来58668条数据

select a.orderCloseApplyId, a.orderCode, a.status, a.remark, o.orderType, a.operLog, a.ownerPartyId, a.owner
from orderCloseApply a inner join order o on a.orderCode = o.orderCode 
where o.orderType = 20 AND o.ownerPartyId  in (568269285)
查出来3条数据

结论:CASE WHEN会把这两个表的交集结果的每一条数据去做匹配:
1. CASE WHEN o.orderType = 20 AND o.ownerPartyId not in (568269285) THEN 0    返回: 0=0,可以成功的查出来数据
2. WHEN o.orderType != 20 THEN 0  返回: 0=0,可以成功的查出来数据
3. ELSE 1 返回: 1=0,查不出来数据,就是上面的那三条数据【orderType = 20 AND o.ownerPartyId  in (568269285)】

猜你喜欢

转载自blog.csdn.net/robinson_911/article/details/113400904