Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

randomly picking a field 1

Status
Not open for further replies.

ankursaxena

Programmer
Sep 7, 2001
133
US
How do i write a program which will pick a random record set from a table? i have a table of available numbers and i want to pick one with a where clause stating
where Status='Avail';
?? so that i get a random available telephone number to be assigned?

please help thanx a lot

Ankur
 
I dont think there is a statement for that.

You can do like this:

x = get a random number in your number interval

select *
from table
where number > x
order by number;

use the first line selected
 
If you have an id field in your table (primary index auto-increment) you would use

SELECT @random:=truncate((rand() * 1000),0),phone_number FROM Table WHERE id = @random;


*note the * 1000 = 1000 records put here your number of records.

***************************************
Party on, dudes!
[cannon]
 
Woops quick mod, currently you'll never be able to get your highest record so we need to add 1 to the equation.

SELECT @random:=truncate((rand() * 1000 +1 ),0),phone_number FROM Table WHERE id = @random; ***************************************
Party on, dudes!
[cannon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top