我要根据同一个userid和某些条件,从3个表里面取出出6个count统计值sql 语句怎么写更合理些?

解决方案 »

  1.   

    就是我有3张表 tab1 ,tab2,tab3,每个表里面有些标示植如flag,deleted,
    我需要同时求出:
    select count(*) from tab1 where userid=01 and flag=1,
    select count(*) from tab1 where userid=01 and deleted=0,
    select count(*) from tab2 where userid=01 and flag=1,
    select count(*) from tab2 where userid=01 and flag=0,
    select count(*) from tab3 where userid=01 and flag=1,
    select count(*) from tab3 where userid=01 and deleted=1,诸如这样的
      

  2.   

    select distinct ttt.userid,
    (select count(*) from tab1 where tab1.userid=ttt.userid and flag=1) tmp1,
    (select count(*) from tab1 where tab1.userid=01 and deleted=0) tmp2,
    (select count(*) from tab2 where tab2.userid=01 and flag=1) tmp3,
    (select count(*) from tab2 where tab2.userid=01 and flag=0) tmp4,
    (select count(*) from tab3 where tab3.userid=01 and flag=1) tmp5,
    (select count(*) from tab3 where tab3.userid=01 and deleted=1) tmp6
    FROM tab1 ttt