mysql中case when判断两个字段的实现
select
COUNT(distinct case when p.lab like '%L%' then
(case when p.bar_code like '%T%' then p.bar_code else null end)
else null end ) countBarCode
from t_plan p
说明:在case when的then位置嵌套case when,能够实现两个条件对某一个字段的过滤。
上面的例子是首先根据lab字段过滤,然后根据bar_code过滤,返回bar_code ,之后distinct 对返回的bar_code去重,最后count对不重复的bar_code做了一个统计返回。