Jump to content


Photo

Select random row if value is not found


  • Please log in to reply
5 replies to this topic

#1 aviddv1

aviddv1
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 27 September 2006 - 11:35 PM

Hey there,

I'm trying to write a query that will select 1 random row from a table only if the value 0 does not exist in a field called random.  If 0 does exist then i want it to return that row instead of randomly selecting one.

thanks,
ward

#2 fenway

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

Posted 28 September 2006 - 12:21 AM

You can probably cheat with a clever UNION statement and a LIMIT clause, but why not just do this with multiple queries?
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 28 September 2006 - 02:35 AM

Hey there,

I'm trying to write a query that will select 1 random row from a table only if the value 0 does not exist in a field called random.  If 0 does exist then i want it to return that row instead of randomly selecting one.

thanks,
ward


You should also be able to use the following
SELECT * FROM tablename ORDER BY IF(random = 0, -1, RAND()) ASC LIMIT 1


#4 fenway

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

Posted 28 September 2006 - 02:45 PM

You should also be able to use the following

SELECT * FROM tablename ORDER BY IF(random = 0, -1, RAND()) ASC LIMIT 1

How would that work? Which records would it examine?  And I didn't that that you use an expression in an ORDER BY clause.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#5 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 28 September 2006 - 03:10 PM

You should also be able to use the following

SELECT * FROM tablename ORDER BY IF(random = 0, -1, RAND()) ASC LIMIT 1

How would that work? Which records would it examine?  And I didn't that that you use an expression in an ORDER BY clause.


Just as when you "ORDER BY RAND()" each row is ordered based on the evaluation of the expression. In this case the return value of RAND().

ORDER BY IF(random = 0, -1, RAND())
When MYSQL encounters a row whose "random" field contains the value 0 it will be ordered based on the value -1. Otherwise, the row is ordered based on the value returned by RAND().

Since -1 will be lower than any RAND() (between 0 and 1) value, if a row in the table has a "random" value of 0 it will be first in the result. If not, one of the other fields will be the first in the result and that will be based on the RAND() value.

The LIMIT 1 of course gives us only the first record.

#6 fenway

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

Posted 28 September 2006 - 06:31 PM

Of course it can use an expression... my brain wasn't working.  Very clever trick!
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users