clear markdown compare help donate or buy diddy dollings comments/suggestions/bugs a leap of faith? bible365
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 809167 fiddles created (9775 in the last week).

select version();
version()
5.6.45
 hidden batch(es)


CREATE TABLE test -- 22 records ( the_word TEXT, no_of_times INTEGER ); INSERT INTO test VALUES ('keyword1', 100); INSERT INTO test VALUES ('keyword2', 90); INSERT INTO test VALUES ('keyword3', 80); INSERT INTO test VALUES ('keyword4', 70); INSERT INTO test VALUES ('keyword5', 60); INSERT INTO test VALUES ('keyword6', 50); INSERT INTO test VALUES ('keyword7', 40); INSERT INTO test VALUES ('keyword8', 40); INSERT INTO test VALUES ('keyword9', 40); INSERT INTO test VALUES ('keyword10', 40); INSERT INTO test VALUES ('keyword11', 40); INSERT INTO test VALUES ('keyword12', 40); INSERT INTO test VALUES ('keyword13', 40); INSERT INTO test VALUES ('keyword14', 40); INSERT INTO test VALUES ('keyword15', 40); INSERT INTO test VALUES ('keyword16', 40); INSERT INTO test VALUES ('keyword17', 40); INSERT INTO test VALUES ('keyword18', 40); INSERT INTO test VALUES ('keyword19', 40); INSERT INTO test VALUES ('keyword20', 40); INSERT INTO test VALUES ('keyword21', 40); INSERT INTO test VALUES ('keyword22', 40);
 hidden batch(es)


SELECT the_word, no_of_times FROM test ORDER BY no_of_times DESC, RAND() LIMIT 15; -- Tested several times - keywords1-6 always there, -- then random between 7 and 22 (9 of these records)
the_word no_of_times
keyword1 100
keyword2 90
keyword3 80
keyword4 70
keyword5 60
keyword6 50
keyword19 40
keyword10 40
keyword13 40
keyword21 40
keyword8 40
keyword7 40
keyword9 40
keyword15 40
keyword17 40
 hidden batch(es)


CREATE TABLE test1 (the_word VARCHAR(10), no_of_times INTEGER); INSERT INTO test1 VALUES ('keyword1', 1), ('keyword1', 2), ('keyword1', 2), ('keyword1', 1), ('keyword1', 1), ('keyword1', 1), ('keyword1', 1), ('keyword1', 1), ('keyword2', 1), ('keyword2', 2), ('keyword2', 2), ('keyword2', 1), ('keyword2', 1), ('keyword2', 1), ('keyword2', 1), ('keyword2', 1), ('keyword2', 1), ('keyword2', 1), ('keyword2', 1), ('keyword3', 3), ('keyword3', 3), ('keyword3', 2), ('keyword3', 2), ('keyword3', 1);
 hidden batch(es)


SELECT the_word, COUNT(no_of_times), SUM(no_of_times) FROM test1 GROUP BY the_word ORDER BY COUNT(*) DESC, RAND() LIMIT 15
the_word COUNT(no_of_times) SUM(no_of_times)
keyword2 11 13
keyword1 8 10
keyword3 5 11
 hidden batch(es)


SELECT the_word, COUNT(no_of_times), SUM(no_of_times) AS sm FROM test1 GROUP BY the_word ORDER BY SUM(no_of_times) DESC, RAND() LIMIT 15 -- can also use sm
the_word COUNT(no_of_times) sm
keyword2 11 13
keyword3 5 11
keyword1 8 10
 hidden batch(es)