Is there an alternative for this?
This is my code
function loadQ() {
db.transaction(function(t) {
t.executeSql('SELECT * FROM q ORDER BY RAND()', [], qDataHandler, errorHandler);
});
};
Now, this doesn’t work. It works if I remove the Rand() and replace it with LIMIT 1. But I want the result to be randomized.
I am thinking of running a query for a total rows and then create a function that picks numbers from 0 to total rows and then use the result in my loadQ as ID. But i think this is a tedious work. Am wondering if you have any better ideas for this?
many thanks.
If you use the random value as the PK, it’ll fail if you have gaps in your rows.
Here’s a better solution:
Query for a COUNT() of rows.
Generate a random number from 0 to the count
Use the result in
LIMIT 1 OFFSET ?