i have an id table like this from 1 to 30
| id |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| . |
| . |
| . |
| 30 |
what i want to do is get 10 random ids from it in a string like this
(1,2,3,4,5,6,7,8,9,10)
what i figured out was
https://www.db-fiddle.com/f/3gTHLSTdFEac7dKbDcU43d/3
https://www.db-fiddle.com/f/3gTHLSTdFEac7dKbDcU43d/4
SELECT
CONCAT_WS(", ",
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids)),
CEIL(RAND() * (SELECT MAX(id) FROM ids))
) AS random_ids
is there a faster and more efficient way without using ORDER BY RAND()
?
and is there also a way to make them unique like having a gap between them?
Aucun commentaire:
Enregistrer un commentaire