PROGRAMMING

MINTE9
REMEMBERS




Order by rand

Order by rand is very slow. Order by index (random) is recommended.
 
SELECT * FROM article ORDER BY RAND() LIMIT 1;
 
ALTER TABLE article ADD random FLOAT NOT NULL;

ALTER TABLE ADD INDEX random (random);

UPDATE article SET random = RAND();

INSERT INTO article (..., random) VALUES (..., RAND());

SELECT * FROM article WHERE random > 

    (SELECT RAND() as rand) ORDER BY random LIMIT 1;

Daily Rand

Get random item by seed (day).
 
SELECT * FROM article ORDER BY rand("20190131") # date(Ymd)    

Order by array

Get items from array, then order by field.
 
SELECT * FROM documents 

    WHERE id IN (3, 7, 5) ORDER BY FIELD (id, 3, 7, 5)

Questions    
2. Concat fields
01 . Order by rand
Last update:   28-05-2021

        A B C D E F
🔔
1/4