select
g.gid,
g.name
from groups g
inner join groupmembers gm ON gm.memberId = 1 AND g.gid = gm.groupId
select
g.name,
COUNT(gm.id) as CountGM
from groups g
inner join groupmembers gm ON gm.memberId = 1 AND g.gid = gm.groupId
group by g.name
select
[ id пользователя]
ISNULL(U.ID, system.id] as [ID сообщения]
from tbl_notice N
left join user U ON U.ID=N.[id сообщения] and type="user"
left join Systems S ON S.ID=N.[id сообщения] and type="System"