请帮我解释为什么这个查询不起作用。我怎样才能得到结果?请帮帮我。
Mysql查询:
SELECT ( select count('*') from `lostpets` where `lostpets`.`type` = 'Lost' ) as lost
, ( select count('*') from `lostpets` where `lostpets`.`type` = 'Found' ) as found
,
FROM `lostpets`;
答案 0 :(得分:0)
子查询返回计数,因此您不需要外部查询中的from
子句,并且您不需要*
周围的单引号:
SELECT (select count(*) from `lostpets` where `lostpets`.`type` = 'Lost') as lost,
(select count(*) from `lostpets` where `lostpets`.`type` = 'Found') as found
或者,您可以使用条件计数,并在没有子查询的情况下执行此操作:
SELECT count(if(`type` = 'Lost',1,null)) as lost,
count(if(`type` = 'Found',1,null)) as found
FROM `lostpets`