如何计算用3列选择不同的mysql

时间:2015-03-13 10:12:00

标签: mysql mysqli routines

我的表格如下:

ID_USER USERNAME_USER REF_USER STATUS_AKTIF
1       a                      aktif
2       b             a        tidak
3       c             a        aktif
4       d             b        aktif
5       e             a        aktif

我使用这段代码:

select REF_USER, count(distinct USERNAME_USER) as TOTAL from TABLE where (REF_USER IS NOT NULL AND REF_USER!="" AND STATUS_AKTIF='aktif') group by REF_USER

但我只得到:

REF_USER TOTAL
a        2
b        1

我怎么能删除b?因为用户b STATUS_AKTIF是tidak

1 个答案:

答案 0 :(得分:0)

只需按

添加一个组
SELECT count(*) as total, username_user FROM table 
WHERE (REF_USER IS NOT NULL AND REF_USER!="" AND STATUS_AKTIF='aktif') 
GROUP BY username_user