CREATE PROCEDURE `up_GetPhotosThumbSlider`(KY INT, HOWMANY INT)
BEGIN
DECLARE OverCount INT;
DECLARE UnderCount INT;
SET @id = KY;
SET @odd = (HOWMANY MOD 2);
SET @th = FLOOR(HOWMANY/2);
SET @sql_all = 'SELECT * from tblPhotos';
SELECT COUNT(*) INTO OverCount from tblPhotos WHERE `Key` > @id;
SELECT COUNT(*) INTO UnderCount from tblPhotos WHERE `Key` < @id;
IF OverCount < @th THEN
SET @thb = HOWMANY - OverCount - 1;
ELSE
SET @thb = @th;
END IF;
IF UnderCount < @th THEN
SET @tht = HOWMANY - UnderCount - 1;
ELSE
IF @odd = 1 THEN
SET @tht = @th;
ELSE
SET @tht = @th-1;
END IF;
END IF;
SET @st4 = CONCAT(@sql_all,' WHERE `Key` = ',@id);
SET @st1 = CONCAT(@sql_all,' WHERE `Key` > ',@id,' ORDER BY `Key` ASC LIMIT ',@tht);
SET @st2 = CONCAT(@sql_all,' WHERE `Key` < ',@id,' ORDER BY `Key` DESC LIMIT ',@thb);
SET @st3 = CONCAT('(',@st4,') UNION (',@st2,') UNION (',@st1,') ORDER BY `Key` ASC');
PREPARE qs from @st3;
EXECUTE qs;
DROP PREPARE qs;
END