近期又开始写原生sql,比较麻烦,筛选门店同一个字段不同值数量,话不多说上代码:
$sql = "SELECT
COUNT(distinct(if(type = 1, null, store_code))) as has_view,
COUNT(distinct(if(type in (2,3), null, store_code))) as has_check,
store_code
FROM data_all
WHERE deleted_at = 0
AND store_code in ($storeCodes) // 门店编码集合IN查询
GROUP BY store_code";
这只是sql,重点的就是COUNT里面的代码,可以通过if判断做筛选,里面直接可以使用in查询比较方便。
这里要简单的需要注意的就是,有count和sum等计算类的方法,需要group by一下才可以,否则汇报错。
推荐