简体   繁体   English

如何在MySQL中使用case语句

[英]How to use case statement in MySQL

i am using three query's in mysql statement using where condition for example,how to make this as a single query 我正在使用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 

You don't need a CASE statement to do this (although it'd possibly be somewhat easier to read than using IF if the conditions contained OR s), the intent is better conveyed with an IF : 你并不需要一个CASE语句来做到这一点(虽然它可能会在一定程度上更容易比使用阅读IF如果包含了条件OR 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM