In MySQL I can use the RAND() function, is there any alternative in SQLite 3?
If you can find the answer within four minutes of asking the question, you probably shoulnd't have asked it at all. Next time: Visit google before stackoverflow.
Emil H
2009-08-10 07:45:46
I disagree. We have two bits of info here now, how to select a single record randomly,how to list all the records randomly. I have never needed to do either, but if I do, now I know how. I also know that MySQL does it different to SQLlite. A super technical question would be more impressive, but less useful.
Chris Huang-Leaver
2009-08-10 08:01:27
My first thought was that there wasn't any function to order results randomly, or if there was such a feature / function it would be considerable more obscure - that's what happens with SQLite triggers for instance.
Alix Axel
2009-08-10 09:30:52
+2
A:
using random():
SELECT foo FROM bar
WHERE id >= (abs(random()) % (SELECT max(id) FROM bar))
LIMIT 1;
dfa
2009-08-10 07:44:37
Yes this solution is faster, but assumes id starts at 1 and has no gaps. Otherwise rows that follow gaps are "randomly" chosen more frequently than other rows.
Bill Karwin
2009-08-10 08:28:49