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 < 512) 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 > 512 ) ORDER BY position ASC LIMIT 1