-- cleanup DROP TABLE IF EXISTS table_source; DROP TABLE IF EXISTS table_result; -- create source table CREATE TABLE table_source( id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT, account INT(11) NOT NULL, INDEX(account) ); -- fill table SET @@cte_max_recursion_depth = 10000; INSERT INTO table_source(id,account) WITH RECURSIVE cte (id,account) AS ( SELECT 1 AS id, CAST(CEIL(RAND()*10000) AS signed) AS account UNION ALL SELECT id + 1 AS id, CAST(CEIL(RAND()*10000) AS signed) AS account FROM cte WHERE id < 10000 ) SELECT * FROM cte; -- create view CREATE OR REPLACE VIEW view_random AS SELECT * FROM table_source ORDER BY RAND(); -- select 5 random rows (because the view is ordered by RAND() and insert them into a new table CREATE TABLE table_result AS SELECT * FROM view_random LIMIT 5; -- compare this with running this query SELECT * FROM view_random LIMIT 5 -- Result -- -- The view has the first 5 rows ordered by account (wrong) -- The select has the desired result (5 random rows)