我在使用 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)
我在使用 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)
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
您不需要CASE
声明来执行此操作(尽管它可能比使用IF
if 条件包含OR
s 更容易阅读),使用 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