0

我在使用 where 条件的 mysql 语句中使用三个查询,例如,如何将其作为单个查询

SELECT COUNT(*) as A FROM tbl_user WHERE (b=1 AND c=0)  AND (d=1 AND e=1)
SELECT COUNT(*) as M FROM tbl_user WHERE (H=1 AND I=0)  AND (J=1 AND K=1)
4

2 回答 2

1
SELECT 
sum(case when (b=1 AND c=0)  AND (d=1 AND e=1) then 1 else 0 end) as CountA,
sum(case when (H=1 AND I=0)  AND (J=1 AND K=1) then 1 else 0 end) as CountM
 FROM tbl_user 
于 2012-05-15T11:37:43.537 回答
0

您不需要CASE声明来执行此操作(尽管它可能比使用IFif 条件包含ORs 更容易阅读),使用 s 可以更好地传达意图IF

SELECT SUM(IF(b=1 AND c=0 AND d=1 AND e=1, 1, 0)) AS A,
       SUM(IF(H=1 AND I=0 AND J=1 AND K=1, 1, 0)) AS M
FROM tbl_user
于 2012-05-15T11:37:53.877 回答