我希望得到多个条件下的ID

时间:2016-06-10 10:39:12

标签: mysql

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;

这是正确的查询,但我希望在单个查询中如何实现

样本表

enter image description here

1 个答案:

答案 0 :(得分:0)

对结果表使用union

 (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);