SELECT (select statename from state where stateid = cpownerstate) as cpownerstate,
       count(cpownerid)
  FROM cpownerdetail
 where cpownerhashcode=0
 group by cpownerstate;

SELECT (select statename from state where stateid = cpownerstate) as cpownerstate,
       count(cpownerid)
  FROM cpownerdetail
 where cpownerhashcode!=0
 group by cpownerstate;


这是正确的查询,但我希望在单个查询中它怎么可能

样本表:

mysql - 我想获取多个条件下的ID数-LMLPHP

最佳答案

对结果表使用联合

 (select  statename, count(*)
 from state where stateid = cpownerstate
 and cpownerhashcode=0
 group by statename)
 union
 (select  statename, count(*)
 from state where stateid = cpownerstate
 and cpownerhashcod!=0
 group by statename);

10-07 15:38