SELECT
	'perm' type,
	gp.perm_id,
	0 perm_sub_id,
	0 perm_comp_id,
	perm.perm_name,
	null perm_sub_name,
	null perm_comp_name
FROM
	users_groups ug
	INNER JOIN group_perm gp ON gp.group_id = ug.group_id
	INNER JOIN permissions perm ON perm.perm_id = gp.perm_id 
WHERE
	ug.group_id = 1 
	AND ug.user_id = 1

UNION

SELECT
	'sub_perm' type,
	perm.perm_id,
	gsp.perm_sub_id,
	0 perm_comp_id,
	perm.perm_name,
	perm_sub.perm_sub_name perm_sub_name,
	null perm_comp_name
FROM
	users_groups ug
	INNER JOIN group_sub_perm gsp ON gsp.group_id = ug.group_id 
	INNER JOIN permission_submodules perm_sub ON perm_sub.perm_sub_id = gsp.perm_sub_id
	INNER JOIN permissions perm ON perm.perm_id = perm_sub.perm_id
WHERE
	ug.group_id = 1 
	AND ug.user_id = 1

UNION

SELECT
	'comp_perm' type,
	perm.perm_id,
	perm_comp.perm_sub_id,
	perm_comp.perm_comp_id,
	perm.perm_name,
	perm_sub.perm_sub_name,
	perm_comp.perm_comp_name
FROM
	users_groups ug
	INNER JOIN group_comp_perm gcp ON gcp.group_id = ug.group_id
	INNER JOIN permission_components perm_comp ON perm_comp.perm_comp_id = gcp.perm_comp_id
	INNER JOIN permission_submodules perm_sub ON perm_sub.perm_sub_id = perm_comp.perm_sub_id
	INNER JOIN permissions perm ON perm.perm_id = perm_sub.perm_id
WHERE
	ug.group_id = 1 
	AND ug.user_id = 1
	
ORDER BY perm_id, perm_sub_id asc