Du behöver bara ange ORDER BY en gång. mysql_query("SELECT id FROM tb_table ORDER BY num ASC, RAND() LIMIT 1");