select sum(case when userindex='ca8USERID00000002943f262f0600000041' and maillocation='inbox' then 1 else 0 end) count1,
       sum(case when userindex='ca8USERID00000002943f262f0600000041' and maillocation='sendbox' then 1 else 0 end) count2,
       sum(case when userindex='ca8USERID00000002943f262f0600000041' and maillocation='trashbox' then 1 else 0 end) count3,
       sum(case when userindex='caUSERID00000003333f262f0600000036' and maillocation='inbox'  then 1 else 0 end) count4
       sum(case when userindex='caUSERID00000003333f262f0600000036' and maillocation='sendbox'  then 1 else 0 end) count5
       sum(case when userindex='caUSERID00000003333f262f0600000036' and maillocation='trashbox'  then 1 else 0 end) count6
       from mailinfo ;

解决方案 »

  1.   

    你这样写不行吗?select userindex 用户编号,
           sum(case when maillocation='inbox' then 1 else 0 end) 收件箱,
           sum(case when maillocation='sendbox' then 1 else 0 end) 发件箱,
           sum(case when maillocation='trashbox' then 1 else 0 end) 垃圾箱
           from mailinfo group by userindex ;
      

  2.   

    为什么要把userindex写进去?想靠它提高检索速度吗?
    用楼上的方法就好了,它同样利用了索引userindex 。
      

  3.   

    大哥,我知道我错哪了,就把 else 0 改位else null就ok了