-
Jan 11th, 2018, 05:47 AM
#1
Thread Starter
Addicted Member
random record
good day.
I have this table with id number and fullname.
Let say the table have 100,000 records.
I want to create a query that will show randomized select 20 records out of 100,000.
Can anybody show me how? I already researched on google but got no result. Thanks in advance.
-
Jan 11th, 2018, 05:54 AM
#2
Re: random record
The best way to do many things depends on the database, which you haven't told us.
-
Jan 11th, 2018, 06:23 AM
#3
Thread Starter
Addicted Member
Re: random record
oh, i forgot, im using mysql.
just now i saw a query but they said it will be slow if it involves million records
SELECT col1 FROM tbl WHERE RAND() limit 100;
-
Jan 11th, 2018, 06:47 AM
#4
Re: random record
Your table doesn't contain millions of records so no problem.
-
Jan 11th, 2018, 07:21 AM
#5
Thread Starter
Addicted Member
Re: random record
Originally Posted by jmcilhinney
Your table doesn't contain millions of records so no problem.
that could be but supposing it has those number of records
-
Jan 11th, 2018, 07:58 AM
#6
Re: random record
Then the query will be slow.
-
Jan 11th, 2018, 05:23 PM
#7
Re: random record
just a general idea how to select 20 random records out of 100000 or 2000000 or whatever at comparable speed:
do until there are 20 numbers stored
generate a random number between the lowest ID and the highest ID
if the generated number is an ID in the table, and not yet stored then store it
loop
now you have 20 distinct record id's
do not put off till tomorrow what you can put off forever
-
Jan 12th, 2018, 07:44 AM
#8
Re: random record
Originally Posted by genlight
oh, i forgot, im using mysql.
just now i saw a query but they said it will be slow if it involves million records
SELECT col1 FROM tbl WHERE RAND() limit 100;
Hi,
try it this way...
Code:
$x = 20;
$sql = "SELECT Field1,
Field2,
Field3
FROM table1
ORDER BY RAND()
LIMIT ".$x;
$query = mysql_query($sql);
this would be for Access...
Code:
SELECT TOP 20 Orders.OrderID, Orders.CustomerID
FROM Orders
ORDER BY rnd(ISNULL(Orders.OrderID) * 0 +1);
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|