SELECT *,getparentname_bytype(unit.id, 1) AS spolka , getparentid_bytype(unit.id, 1) AS spolka_id, getparentname_bytype(unit.id, 2) AS pion, getparentname_bytype(unit.id, 3) AS departament, get_przelozony(unit.id) AS przelozony, get_przelozony_email(unit.id) AS przelozony_email, email, unit.name AS "stanowisko" FROM unit LEFT JOIN employeeunit ON unit.id = employeeunit.unit_id LEFT JOIN employee ON employeeunit.employee_id = employee.id WHERE unittype_id=5 AND lastname<>"" AND getparentid_bytype(unit.id, 1)=2184 ORDER BY spolka DESC, lastname
a tutaj mam poszegolne funkcje najpierw getparentid_bytype
FUNCTION `getparentid_bytype`(id1 int(11), tp int(11)) RETURNS int(11) BEGIN SET @parent_id = id1, @fuse = 1, @name1="", @unittype_id=0; petla: while 1 do SELECT parent_id, unittype_id, name INTO @parent_id1, @unittype_id, @name1 FROM unit WHERE id=@parent_id; IF @unittype_id = tp AND @parent_id<>id1 AND @fuse>1 THEN LEAVE petla; END IF; SET @parent_id=@parent_id1; IF @parent_id=22 THEN SET @name1=""; LEAVE petla; END IF; SET @fuse = @fuse + 1; IF @fuse > 100 THEN LEAVE petla; END IF; END WHILE petla; RETURN @parent_id;
getparentname_bytype
FUNCTION `getparentname_bytype`(id1 int(11), tp int(11)) RETURNS varchar(255) CHARSET cp1250 BEGIN SET @parent_id = id1, @fuse = 1, @name1="", @unittype_id=0; petla: while 1 do SELECT parent_id, unittype_id, name INTO @parent_id1, @unittype_id, @name1 FROM unit WHERE id=@parent_id; IF @unittype_id = tp AND @parent_id<>id1 AND @fuse>1 THEN LEAVE petla; END IF; SET @parent_id=@parent_id1; IF @parent_id=22 THEN SET @name1=""; LEAVE petla; END IF; SET @fuse = @fuse + 1; IF @fuse > 100 THEN LEAVE petla; END IF; END WHILE petla; RETURN @name1;
i oczywiscie getprzelozony_mail
FUNCTION `get_przelozony_email`(id1 int(11)) RETURNS varchar(255) CHARSET cp1250 BEGIN SET @parent_id = id1, @fuse = 1, @name1="", @unittype_id=0; petla: while 1 do SELECT parent_id, unittype_id, unit.id, name, employee.lastname, employee.firstname, employee.id,email INTO @parent_id, @unittype_id, @id, @name1, @lastname, @firstname, @eid, @email FROM unit LEFT JOIN employeeunit ON unit.id = employeeunit.unit_id LEFT JOIN employee ON employeeunit.employee_id = employee.id WHERE unit.id=@parent_id ; IF @unittype_id = 5 AND @id<>id1 AND @lastname<>'' THEN LEAVE petla; END IF; IF @parent_id=22 THEN SET @name1=""; LEAVE petla; END IF; SET @fuse = @fuse + 1; IF @fuse > 100 THEN LEAVE petla; END IF; END WHILE petla; RETURN concat(@email) END */;;
po namyśle wydaje się że jednak pominąłem jedną funkcję i na 100 procent to ona jest problematyczna
BEGIN SET @parent_id = id1, @fuse = 1, @name1="", @unittype_id=0; petla: while 1 do SELECT parent_id, unittype_id, unit.id, name, employee.lastname, employee.firstname INTO @parent_id, @unittype_id, @id, @name1, @lastname, @firstname FROM unit LEFT JOIN employeeunit ON unit.id = employeeunit.unit_id LEFT JOIN employee ON employeeunit.employee_id = employee.id WHERE unit.id=@parent_id ; IF @unittype_id = 5 AND @id<>id1 AND @lastname<>'' THEN LEAVE petla; END IF; IF @parent_id=22 THEN SET @name1=""; LEAVE petla; END IF; SET @fuse = @fuse + 1; IF @fuse > 100 THEN LEAVE petla; END IF; END WHILE petla; RETURN concat(@id,' ',@firstname, ' ',@lastname); END
ok temat rozwiązany dodanie limit 1 w dwóch ztych funkcji po select into ..... where ... spowodowało że wszystko zaczęło działać pozdrawiam