Re: Fetch 6 records from a table randomly

i have one table “documents” and i want to fetch 6 records randomly is
there any solution reply me

Yes, clearly there is a solution.

However your question fails to be “smart” on a number of counts. By
omitting
critical details (e.g. what sort of table? in a database or an array?
what
sort of database?) then nobody can give you a specific answer. Also, you
haven’t given any indication of which avenues you’ve already explored,
or
indeed of having made any effort at all on your own behalf to try to
solve
the problem before posting. Maybe you have, but the important thing is
you
haven’t shown that you have. So why do you expect someone on the list
to
take more effort in replying than you did yourself in posting?

To illustrate this, let me try making up a question or two which you
could
have asked instead. These may or may not be anything like the actual
problem
you have.

“I have a table ‘documents’ stored in a MySQL database that I’m
accessing
using ActiveRecord. I’d like to be able to pick 6 rows from that table
at
random. I can read all the rows into an array using Document.find(:all),
but
I haven’t been able to find a function in Ruby to pick a random element.
Could someone suggest where I should look? Thanks, Pragash”

In this case, someone might point you to the Kernel#rand method, and/or
to
the many Ruby documentation sources.

However, your problem could be completely different:

"I have a table ‘documents’ stored in a MySQL database that I’m
accessing
using ActiveRecord. I’d like to be able to pick 6 rows from that table
at
random. I thought about reading the entire table into an array using
Document.find(:all), and then using rand(N) to pick elements from it,
but
unfortunately the table has over 1 million rows and is too large to be
read
into RAM. There are many gaps in the ‘id’ sequence column, so I can’t do
Document.find(rand(N)+1) either.

Could someone suggest an efficient way to do this? Thanks, Pragash"

This question is completely different. It’s not about Ruby but about
MySQL,
since really you want to know if there’s a SQL statement which can ask
MySQL
to retrieve rows at random. You may find someone on this list who knows
the
answer, or you may be referred to a MySQL mailing list.

To get more chance of a useful response you should also post relevant
details of your environment (which operating system and version; which
versions of Ruby, MySQL and ActiveRecord you’re using), samples of code
you’ve run, and the errors you got back, copied and pasted verbatim.

The following document gives lots of really good, practical advice on
asking
questions to a mailing list in a way which elicits the most useful
response:
http://www.catb.org/~esr/faqs/smart-questions.html

I strongly suggest you read it.

Regards,

Brian.

Dang, Brian, you surely do not pull your punches. I found what you said
to be clear, polite and very much to the point. Thanks for saying this
kind of thing which needs occasional reinforcement.

As to the question itself, each client server application seems to
implement SQL standards in a non standard way. That is to say that
there is always some proprietary bit here or there. The SQL in oracle
might be:

select * from mytable where rownum = 123

and have the ruby random number generator insert a random value.

Lloyd L. wrote:

As to the question itself, each client server application seems to
implement SQL standards in a non standard way. That is to say that
there is always some proprietary bit here or there. The SQL in oracle
might be:

select * from mytable where rownum = 123

and have the ruby random number generator insert a random value.

I have used this differently and found that it is not exactly as I said.
The rownum column in oracle exists only in a result set so it will work
for row 1 but no other UNLESS…

try this: (I am not at a system with oracle but I want to send this
before I forget.)

select * from
(select * from mytable) – gets it all
where rownum = 123

This way, it has a full result set from which to select. Rather clunky
but I only looked at this between meetings.