I have a fairly simple SQL (MySQL):
SELECT foo FROM bar ORDER BY rank, RAND()
I notice that when I refresh the results, the randomness is s
The RAND()
can not be refresh for each row. A possible solution might be:
SELECT foo FROM bar ORDER BY rank, CHECKSUM(NEWID())
RAND() is only executed once per query. You can verify this by looking at the result set.
If you're trying to get a randomized order, you should be using either NEWID()
or CHECKSUM(NEWID())
.
WITH T AS ( -- example using RAND()
SELECT 'Me' Name UNION SELECT 'You' UNION SELECT 'Another'
)
SELECT Name, RAND()
FROM T;
WITH T AS ( -- example using just NEWID()
SELECT 'Me' Name UNION SELECT 'You' UNION SELECT 'Another'
)
SELECT Name, NEWID()
FROM T;
WITH T AS ( -- example getting the CHECKSUM() of NEWID()
SELECT 'Me' Name UNION SELECT 'You' UNION SELECT 'Another'
)
SELECT Name, CHECKSUM(NEWID())
FROM T;