问题
I'm experimenting with the built in SQL support in the Safari Browser and I want to select a random query via Javascript.
SELECT * FROM questions ORDER BY random()
Returns not authorized to use function: random
See this screenshot.
Any suggestions?
回答1:
Query using a non-random order, then shuffle the results:
tx.executeSql('SELECT * FROM questions',[], function(tx, resultSet) {
var resultArray = [];
for(var i=0; i < resultSet.rows.length; i+=1) {
resultArray.push(resultSet.rows.item(i));
}
var shuffledArray = shuffle(resultArray);
// do something with the shuffled array...
});
Where shuffle()
could be something like this: https://stackoverflow.com/a/962890/490560
回答2:
Perhaps something like the following would work:
SELECT *
FROM (SELECT RANDOM() as RANDOM_NUM, Q.*
FROM QUESTIONS Q)
ORDER BY RANDOM_NUM
Share and enjoy.
来源:https://stackoverflow.com/questions/3500842/select-random-row-from-safaris-client-side-database