Jump to content

Archived

This topic is now archived and is closed to further replies.

pluginbaby

mysql - select command

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Sure... add an ORDER BY RAND() before the LIMIT clause.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.