Assuming you have an autoincremented id, you can do SELECT a. FROM SELECT a. FROM articulos a WHERE cat ArtCat ORDER BY id DESC LIMIT a ORDER BY RAND LIMIT The key idea is the subquery to get the last entries, and then the final query to get the random rows.