Jump to content


Photo

mysql - select command


  • Please log in to reply
2 replies to this topic

#1 pluginbaby

pluginbaby
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 23 August 2006 - 05:21 PM

I got a question:

Imagine this table:
id | number
1 | 1
2 | 2
3 | 3
4 | 2
5 | 6

now I make a select in php:

$str = "select id from table X where number=2 limit 1";
$result=mysql_query($str) or die (mysql_error());
$line = mysql_fetch_row ($result);
$id = $line[0];

when I do this, the database selects the first possible id where the number=2.
What I want to do is make the database select a random id where the number=2.
So it would response with id = 2 sometimes, and other times with id = 4

Is this possible?

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 23 August 2006 - 06:00 PM

Sure... add an ORDER BY RAND() before the LIMIT clause.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 pluginbaby

pluginbaby
  • Members
  • PipPipPip
  • Advanced Member
  • 37 posts

Posted 23 August 2006 - 06:24 PM

thank you, I appreciate your help, it works perfectly now  ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users