如下四条sql语句,只有where条件不同,怎样写能让他们更加优化,速度,速度,我要将查询出来的数据放在一个集合中,appID,serviceCode,wayID,都一样,只不过count(ID)不一样select appID,serviceCode,wayID,count(ID) from CupSmsMT where receipt=0 group by appID,serviceCode,wayIDselect appID,serviceCode,wayID,count(ID) from CupSmsMT where receipt>=0 group by appID,serviceCode,wayIDselect appID,serviceCode,wayID,count(ID) from CupSmsMT whereresult>=0 group by appID,serviceCode,wayIDselect appID,serviceCode,wayID,count(ID) from CupSmsMT where result=0 group by appID,serviceCode,wayID

解决方案 »

  1.   

     其实我就是想要后的appID,serviceCode,wayID,count(ID),count(ID),count(ID),count(ID)(不同查询条件获得的count)放入另一个数据表中,怎么做呢
      

  2.   

    神马优化,where就一个条件,优化啥?
    是要把四条语句的结果合在一起吗?
    你的条件有重复,取>=0,在外面判断好了,要不就直接用union all
    或者在select处判断select appID,serviceCode,wayID,
           sum(case when receipt=0 then 1 else 0 end) as rec0,
           sum(case when receipt>0 then 1 else 0 end) as rec1, 
           sum(case when result=0 then 1 else 0 end) as res0, 
           sum(case when result>0 then 1 else 0 end) as res1
      from CupSmsMT
     where receipt >= 0 or result >= 0
     group by appID,serviceCode,wayID
      

  3.   

    select appID,serviceCode,wayID,
    count(case when receipt=0 then 1 else null end),
    count(case when receipt>=0 then 1 else null end),
    count(case when result>=0 then 1 else null end),
    count(case when result=0 then 1 else null end) 
    from CupSmsMT  group by appID,serviceCode,wayID