php - not able to select count with case condition -
i have 2 tables:
user_groups
id name created_user 1 gn1 3 2 gn2 3
user_group_has_users
group_id user_id 2 5 2 6
i want count users per group , output similar this:
id name user_count 2 gn2 2 1 gn1 0
i tried query
select g.*, count(cg.user_id) user_count user_groups g left join user_group_has_users cg on cg.user_group_id = g.id g.created_user = 3
but returns 1 row. not understand why happening. please me on this.
use query
select g.* ,count(cg.user_id) user_count user_groups g left join user_group_has_users cg on cg.group_id = g.id g.created_user = 3 group g.id order user_count desc ;
Comments
Post a Comment