SELECT s.* FROM stran s LEFT JOIN pripadnost p ON (s.stranid = p.elementid) LEFT JOIN podatek datum ON datum.stranid = s.stranid AND datum.atributid = 18 WHERE deleted = 0 AND s.skrij = 0 AND IF(s.timelimitfrom = 0 OR (s.timelimitfrom <> 0 AND s.timelimitfrom <= UNIX_TIMESTAMP()), 1, 0) = 1 AND IF(s.timelimitto = 0 OR (s.timelimitto <> 0 AND s.timelimitto >= UNIX_TIMESTAMP()), 1, 0) = 1 AND (s.parent = 5901 OR p.stranid = 5901) AND s.tipid IN (3,4,5,6,7,8) GROUP BY s.stranid ORDER BY s.red ASC