473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

retrieving random values

304 100+
hi,
Suppose i have a table say football,
then depending on the country i have some clubs,
i.e. england - manu,chelsea,arsenal,...
spain - barca,real,zaragova...
italy - ac milan,inter, juventus...

now if i want to select some clubs randomly i.e. say two random clubs from each country, then how should i go about it?
Apr 9 '07 #1
8 1393
code green
1,726 Expert 1GB
then depending on the country i have some clubs,
This is meaningless. What is your table structure
Apr 10 '07 #2
gomzi
304 100+
This is meaningless. What is your table structure
Just forget the whole thing. I would like to know whether its possible to pick some random rows from a table.

i.e. table_A has 10 records. i would like 2 records to be picked from the table at random every time a select is made on the table.
Apr 10 '07 #3
code green
1,726 Expert 1GB
Please do not give up. You have a legitimate and interesting problem. It is just that you did not provide enough information to work on.
Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `table_a`
  2.          ORDER BY RAND() LIMIT 2
or
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `table_a`
  2.           WHERE `field`='somevalue'
  3.           ORDER BY RAND() LIMIT 2
Apr 10 '07 #4
gomzi
304 100+
Please do not give up. You have a legitimate and interesting problem. It is just that you did not provide enough information to work on.
Try this
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `table_a`
  2.          ORDER BY RAND() LIMIT 2
or
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM `table_a`
  2.           WHERE `field`='somevalue'
  3.           ORDER BY RAND() LIMIT 2

Thanks a lot mate. Its working perfectly. But for retrieving 2 values i have to say ,
order by rand() limit 3 i.e. one more than that required. any particular reasons for that?
Apr 10 '07 #5
code green
1,726 Expert 1GB
But for retrieving 2 values i have to say ,
order by rand() limit 3 i.e. one more than that required
Sorry, I don't understand your question.
Apr 10 '07 #6
gomzi
304 100+
Sorry, I don't understand your question.

when i specify this query,
SELECT * FROM `table_a`
WHERE `field`='somevalue'
ORDER BY RAND() LIMIT 2
it selects a single row

and when i provide this,
SELECT * FROM `table_a`
WHERE `field`='somevalue'
ORDER BY RAND() LIMIT 3

it selects two rows.

that is , depending on the limit it selects the number of rows .

but the limit has to be one more than that required.cant understand as to why its that way.
Apr 10 '07 #7
code green
1,726 Expert 1GB
I can only suggest that your WHERE condition is filtering out the other record
Apr 11 '07 #8
gomzi
304 100+
I can only suggest that your WHERE condition is filtering out the other record

I dont think that the problem is with the where condition because, when i increase the limit i get more values, which means that there are many values that are returned based on that where condition.

anyway, its fine ...
thanks a lot mate....m getting the random values which i needed. so no probs as of now!!
Apr 11 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: james blair | last post by:
Hi I am generating a random number using random.randint(1,10000000000) Whats the possibility that the numbers generated will be same when generated by 100 users at the same time? Whats the best...
7
by: Robin Becker | last post by:
We've been queried about the randomness of some filenames we're producing. I worked through and seemed to satisfy myself that random is being initialised from the system time in C time(&now)...
2
by: Thomas T. Thai | last post by:
I would like to select a random record from a group of records so I'd end up with one random record per group: CREATE TABLE randtest ( catnum int, title varchar(32) ); INSERT INTO randtest...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
12
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could post a few lines of code which would produce random colors, which will be used in defining different regions on a mesh. So in addition to having n...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
0
by: Andy | last post by:
Hi All. I'm working for a company that has set out a guideline for retrieving data from a database. Nobody can explain to me the reason for the following. When retrieving a set of records...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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...

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.