select gid,count(distinct mid) from members group by gid
mysql> SELECT IFNULL(NULL,10); mysql> 10
select a.gid,a.t_m,ifnull(b.h_c,0) as hc,(a.t_m-ifnull(b.h_c,0)) as l_c from (select mem.gid as gid,count(distinct mid) as t_m from members mem group by gid) as a left join (select gid,count(distinct mid) as h_c from members_history group by gid) as b on a.gid = b.gid;