使用select Count的MySql案例查询
我正在编写一个简单的case语句,如果返回的行数为1或更多,则返回值1;如果未找到任何行,则返回值0 我在下面列出的简单查询中遇到语法错误使用select Count的MySql案例查询,mysql,Mysql,我正在编写一个简单的case语句,如果返回的行数为1或更多,则返回值1;如果未找到任何行,则返回值0 我在下面列出的简单查询中遇到语法错误 SELECT CASE (select count(*) from account where account_id = 12 >0) then 1 else 0 end; 如果条件为真,MySQL将返回1,如果条件为假,MySQL将返回0 如果条件为真,MySQL将返回1,如果条件为假,MySQL将返回0 当查询为true时,我发现我漏掉了这个词
SELECT CASE (select count(*) from account where account_id = 12 >0) then 1 else 0 end;
如果条件为真,MySQL将返回
1
,如果条件为假,MySQL将返回0
如果条件为真,MySQL将返回
1
,如果条件为假,MySQL将返回0
当查询为true时,我发现我漏掉了这个词
SELECT CASE (select count(*) from .account where account_id = 12 >0) when true then 1 else 0 end;
当查询为true时,我发现我漏掉了这个词
SELECT CASE (select count(*) from .account where account_id = 12 >0) when true then 1 else 0 end;
这个
只有在语法上有效,因为您错误地将12与0进行比较。这当然是一个进步:
SELECT CASE ((select count(*) from .account where account_id = 12) > 0) when true then 1 else 0 end;
但是,当您可以测试是否存在时,为什么需要统计所有记录:
SELECT CASE (exists (select 1 from .account where account_id = 12)) when true then 1 else 0 end;
如果存在,最后一行将找到第一行,如果存在,则返回一行,否则返回0。
只有在语法上有效,因为您错误地将12与0进行比较。这当然是一个进步:
SELECT CASE ((select count(*) from .account where account_id = 12) > 0) when true then 1 else 0 end;
但是,当您可以测试是否存在时,为什么需要统计所有记录:
SELECT CASE (exists (select 1 from .account where account_id = 12)) when true then 1 else 0 end;
最后一行将查找第一行(如果存在),如果存在,则返回一行,否则返回0