SELECT 
	node_id,
    node_parent_node_id,
    parent.setting_show_in_templates AS templates,
    settings.setting_name AS setting_name,
    cms_junctions.*
    
    
FROM cms_settings AS settings
JOIN cms_nodes ON node_id = settings.setting_node_id
JOIN cms_settings AS parent ON parent.setting_node_id = node_parent_node_id
LEFT JOIN cms_junctions ON settings.setting_node_id = junction_root_node_id AND junction_join_node_id = :node_id
WHERE junction_join_node_id = :node_id OR (
    junction_join_node_id IS NULL AND (
		CONCAT(';',parent.setting_show_in_templates,';') LIKE CONCAT('%;',:template_name,';%') OR
		parent.setting_show_in_templates = ''
	)
)