SELECT product.*,position FROM product, cat_prod WHERE ( cat_prod.cid IN (47) ) AND ( product.prod_id=cat_prod.prod_id ) AND (isactive=1) AND ( position < 351) ORDER BY position DESC LIMIT 1 SELECT product.*,position FROM product, cat_prod WHERE ( cat_prod.cid IN (47) ) AND ( product.prod_id=cat_prod.prod_id ) AND (isactive=1) AND ( position > 351 ) ORDER BY position ASC LIMIT 1