Toggle navigation
Home
New Query
Recent Queries
Discuss
Database tables
Database names
MediaWiki
Wikibase
Replicas browser and optimizer
Login
History
Fork
This query is marked as a draft
This query has been published
by
Jdcc-berkman
.
Toggle Highlighting
SQL
# http://jan.kneschke.de/projects/mysql/order-by-rand/ # https://stackoverflow.com/questions/249301/simple-random-samples-from-a-sql-database ### All non-deleted revisions in the article namespace #SELECT revision.rev_id, page.* FROM revision #JOIN page on revision.rev_page = page.page_id #JOIN (SELECT CEIL(RAND() * (SELECT MAX(rev_id) FROM revision)) AS rev_id) AS random #WHERE revision.rev_id = random.rev_id #AND rev_deleted = 0 AND page_namespace = 0; DELIMITER $$ DROP PROCEDURE IF EXISTS get_rands$$ CREATE PROCEDURE get_rands (IN cnt INT) BEGIN DROP TEMPORARY TABLE IF EXISTS rands; CREATE TEMPORARY TABLE rands ( rand_id INT ); loop_me: LOOP IF cnt < 1 THEN LEAVE loop_me; END IF; INSERT INTO rands SELECT revision.rev_id, page.* FROM revision JOIN page on revision.rev_page = page.page_id JOIN (SELECT CEIL(RAND() * (SELECT MAX(rev_id) FROM revision)) AS rev_id) AS random WHERE revision.rev_id = random.rev_id AND rev_deleted = 0 AND page_namespace = 0; SET cnt = cnt - 1; END LOOP loop_me; END$$ DELIMITER ; CALL get_rands(4); SELECT * FROM rands; ### All non-deleted revisions in the article-talk namespace #SELECT * FROM revision JOIN page on revision.page_id = page.page_id WHERE rev_deleted = 0 AND page_namespace = 1 ORDER BY RAND() LIMIT 10000; ### All non-deleted revisions in the user namespace #SELECT * FROM revision JOIN page on revision.page_id = page.page_id WHERE rev_deleted = 0 AND page_namespace = 2 ORDER BY RAND() LIMIT 10000; ### All non-deleted revisions in the user-talk namespace #SELECT * FROM revision JOIN page on revision.page_id = page.page_id WHERE rev_deleted = 0 AND page_namespace = 3 ORDER BY RAND() LIMIT 10000;
By running queries you agree to the
Cloud Services Terms of Use
and you irrevocably agree to release your SQL under
CC0 License
.
Submit Query
Stop Query
All SQL code is licensed under
CC0 License
.
Checking query status...