Using MySQL Ruby for a website, and trying to use the built-in RAND()
function to get a list of 10 links from a database. Now, on the
server in the command-line, it works just fine:
%mysql> select id from links order by rand() * 10 limit 10;
%±—+
%| id |
%±—+
%| 37 |
%| 26 |
%| 24 |
%| 46 |
%| 53 |
%| 43 |
%| 38 |
%| 23 |
%| 21 |
%| 58 |
%±—+
%10 rows in set (0.00 sec)
Great! But when I try and do this using Ruby and MySQL Ruby. . .
%links = my.query(‘select id from links order by rand() * 10 limit
10;’)
%
%links.each_hash do |row|
%print row[‘id’]
%print “
”
%end
produces:
9
37
10
18
36
22
29
50
11
21
Doesn’t look too bad, I know. The problem comes when I run it a
couple times. Here are some subsequent result sets:
9
10
49
22
53
40
59
11
29
35
48
53
9
10
20
49
30
23
24
13
9
54
50
10
60
11
58
29
32
25
She’s nay very random, Captain! Just to make sure it wasn’t MySQL
itself that was the problem, I went back into the command-line and got
the following:
9 |
10 |
22 |
19 |
30 |
41 |
26 |
11 |
34 |
45 |
25 |
30 |
14 |
46 |
51 |
40 |
20 |
19 |
35 |
57 |
58 |
29 |
38 |
59 |
50 |
13 |
46 |
33 |
39 |
55 |
None of which show the same pattern of repeating - at least from what
I see - 9, 10, and possibly 11 in every set that the MySQL Ruby
version shows. Am I going crazy, or is there something weird in the
way my queries are getting interpreted? Anyone else run into anything
like this with MySQL Ruby, or know how to fix it?
Thanks,
Dave Dembinski