假如要计算满足条件1的总数,然后又要基于条件1的条件下计算满足条件2 的总数,难道有必要查询2次吗?不,这种方法就ok了
SELECT @YOUNAME=COUNT(DISTINCT b.NAME) ,
@var2=COUNT(DISTINCT CASE WHEN b.XXX= 1 THEN b.NAMEEND)--XX
FROM X a
LEFT JOIN Y b ON a.A = b.X
SELECT COUNT(CASE WHEN Condition THEN 1 END) FROM YourTable;
--使用 IFNULL 或 COALESCE 处理 NULL 值:
SELECT COUNT(IFNULL(ColumnName, 0)) FROM YourTable;
SELECT COUNT(COALESCE(ColumnName, 0)) FROM YourTable;
网友评论