MYSQL计数查询不起作用

时间:2017-04-28 14:20:03

标签: mysql

请帮我解释为什么这个查询不起作用。我怎样才能得到结果?请帮帮我。

Mysql查询:

SELECT ( select count('*') from `lostpets` where `lostpets`.`type` = 'Lost' ) as lost
     , ( select count('*') from `lostpets` where `lostpets`.`type` = 'Found' ) as found
     ,  
  FROM `lostpets`;

1 个答案:

答案 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`