DELIMITER $$

DROP FUNCTION IF EXISTS `GET_USER_NAME_BY_ID` $$
CREATE DEFINER=`root`@`%` FUNCTION `GET_USER_NAME_BY_ID`(param_id INT) RETURNS varchar(100) CHARSET utf8

BEGIN
  DECLARE result varchar(100);
  DECLARE _origin_id INT;
  DECLARE _origin_table varchar(10);


SELECT origin_table INTO _origin_table FROM konta WHERE id = param_id;
SELECT origin_id INTO _origin_id FROM konta WHERE id = param_id;

  CASE _origin_table
        WHEN 'user' THEN
        BEGIN
          SET result = (SELECT name FROM `user` WHERE id = _origin_id);
        END;
       WHEN 'admin' THEN
       BEGIN
          SET result = (SELECT name FROM `admin` WHERE id = _origin_id);
       END;

       WHEN 'afilianci' THEN
       BEGIN
          SET result = (SELECT CONCAT(nazwa,' ',imie,' ',nazwisko) FROM `user` WHERE id = _origin_id);
       END;
       ELSE
         SET result = NULL;
  END CASE;

  RETURN result;

END $$

DELIMITER ;




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
DELIMITER $$

DROP FUNCTION IF EXISTS `GET_FULL_USER_NAME_BY_ID` $$
CREATE DEFINER=`root`@`%` FUNCTION `GET_FULL_USER_NAME_BY_ID`(param_id INT) RETURNS varchar(100) CHARSET utf8

BEGIN
  DECLARE result varchar(100);
  DECLARE _origin_id INT;
  DECLARE _origin_table varchar(10);


SELECT origin_table INTO _origin_table FROM konta WHERE id = param_id;
SELECT origin_id INTO _origin_id FROM konta WHERE id = param_id;

  CASE _origin_table
        WHEN 'user' THEN
        BEGIN
          SET result = (SELECT company_name  FROM `user` AS u JOIN user_data AS ud ON u.id=ud.user_id WHERE u.id = _origin_id);
        END;
       WHEN 'admin' THEN
       BEGIN
          SET result = '';
       END;

       WHEN 'afilianci' THEN
       BEGIN
          SET result = '';
       END;
       ELSE
         SET result = NULL;
  END CASE;

  RETURN result;

END $$

DELIMITER ;