473,498 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random select

Hi,

When I do a select with a limit I get always the same records. Usually this
is the desidered effect.

But now I want to do a random select. So every time I do a select I should
get a different set of results.

What is the best way to achieve this effect?

Thanks,
Wim
Jul 23 '05 #1
3 2380
Wim Roffil wrote:
Hi,

When I do a select with a limit I get always the same records. Usually this
is the desidered effect.

But now I want to do a random select. So every time I do a select I should
get a different set of results.

What is the best way to achieve this effect?

Thanks,
Wim


What is your problem? Do you really want random records, or are you
bothered that you get the same records everytime you perform a select?

If its the latter, you should use a WHERE clause in your SELECT and
perhaps also a LIMIT start,rows

Thus,something like

SELECT firstname,lastname FROM myTable WHERE ID>10 LIMIT 5,12

will find all records that have ID>10 and return 12 records, starting
from the 5th one found (thus something like records with ID 15 to 27).

If you want to read random records, request a random record (I'm
assuming you've got a column/cell that is AUTO_INCREMENT that you could
use as reference).

Does that help you any?
randelld
Jul 23 '05 #2

"Randell D." <re******************************@fiprojects.moc > schreef in
bericht news:LpHQd.407506$8l.401215@pd7tw1no...
Wim Roffil wrote:
Hi,

When I do a select with a limit I get always the same records. Usually this is the desidered effect.

But now I want to do a random select. So every time I do a select I should get a different set of results.

What is the best way to achieve this effect?

Thanks,
Wim
What is your problem? Do you really want random records, or are you
bothered that you get the same records everytime you perform a select?


Thank you for your reply.

I need reaaly random records. From a big collection I want to show the
visitors of my webpage just 5 or 10. Because adjacent records may be related
they should be spread over the whole collection. AUTO_INCREMENT is not an
option: it is not already there and the query is not over the whole table
but over a part of it (there is a WHERE clause).

If its the latter, you should use a WHERE clause in your SELECT and
perhaps also a LIMIT start,rows

Thus,something like

SELECT firstname,lastname FROM myTable WHERE ID>10 LIMIT 5,12

will find all records that have ID>10 and return 12 records, starting
from the 5th one found (thus something like records with ID 15 to 27).

If you want to read random records, request a random record (I'm
assuming you've got a column/cell that is AUTO_INCREMENT that you could
use as reference).

Does that help you any?
randelld

Jul 23 '05 #3
d
try
select * from table order by rand() limit 0,5

This doesn't have the best performance for large recordset, but is ok
for small tables
--
Jay
Wim Roffil wrote:
"Randell D." <re******************************@fiprojects.moc > schreef in bericht news:LpHQd.407506$8l.401215@pd7tw1no...
Wim Roffil wrote:
Hi,

When I do a select with a limit I get always the same records.
Usually
this is the desidered effect.

But now I want to do a random select. So every time I do a select
I
should get a different set of results.

What is the best way to achieve this effect?

Thanks,
Wim
What is your problem? Do you really want random records, or are you
bothered that you get the same records everytime you perform a select?
Thank you for your reply.

I need reaaly random records. From a big collection I want to show the visitors of my webpage just 5 or 10. Because adjacent records may be related they should be spread over the whole collection. AUTO_INCREMENT is not an option: it is not already there and the query is not over the whole table but over a part of it (there is a WHERE clause).

If its the latter, you should use a WHERE clause in your SELECT and
perhaps also a LIMIT start,rows

Thus,something like

SELECT firstname,lastname FROM myTable WHERE ID>10 LIMIT 5,12

will find all records that have ID>10 and return 12 records,

starting from the 5th one found (thus something like records with ID 15 to 27).
If you want to read random records, request a random record (I'm
assuming you've got a column/cell that is AUTO_INCREMENT that you could use as reference).

Does that help you any?
randelld


Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13560
by: Rahul Anand | last post by:
Hi all, I am trying to fetch 5 records, randomly picked and in random order from a MySQL table (MySQL version > 3.23). I wrote my SQL Query as SELECT name,id FROM tablename WHERE active =...
5
2204
by: Christopher Brandsdal | last post by:
Hi! I'm not sure, but this might be a question not right for this group. Problem: I have a sql statement that selects all rows with a sertain criteria, an display it on a page. What I need...
12
16345
by: Joseph Shraibman | last post by:
Is there a way to get random rows besides ORDER BY random()? The problem with ORDER BY random() is that is has to get all the rows from the table before the results are returned. ...
7
10624
by: Jean-Francois.Doyon | last post by:
Hello, I'm trying to retrieve a limited number of random rows, and order them by a column, and am not having any luck with that last part: SELECT * FROM tablename ORDER BY random(), id LIMIT...
2
6233
by: Ini | last post by:
Hi, Is there an easy way to pick at random one record out of a recordset and then leave this recordset? I was thinking about doing a recordcount of the recordset, then find at random the...
2
2110
by: Arnau Rebassa | last post by:
Hi everybody, I'm doing the following query: select * from messages order by random() limit 1; in the table messages I have more than 200 messages and a lot of times, the message retrieved...
8
3816
by: Kari Lavikka | last post by:
Hi! I have to select a random row from a table where primary key isn't continuous (some rows have been deleted). Postgres just seems to do something strange with my method. -- -- Use the...
4
2380
by: darrel | last post by:
I can grab a random number in vb.net like this: Dim RandomClass As New Random Dim RandomNumber As Integer RandomNumber = RandomClass.Next(1, 26) However, what I want is a random number. Short...
48
4194
by: Jimmy | last post by:
thanks to everyone that helped, unfortunately the code samples people gave me don't work. here is what i have so far: <% Dim oConn, oRS, randNum Randomize() randNum = (CInt(1000 * Rnd) + 1) *...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7205
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.