MySQL order by rand but result must exist different specific value
WITH 
cte AS ( SELECT id, category, name,
                ROW_NUMBER() OVER (PARTITION BY category ORDER BY RAND()) rn
         FROM test )
SELECT id, category, name
FROM cte
ORDER BY rn &gt; 1, RAND()
LIMIT ? ;


fiddle

For MySQL version below 8 calculate rn in subquery using user-defined variables.