PDA

View Full Version : SQL question


ricksmit
01-16-2003, 01:50 PM
Hey,

I've got a question about a query in MySQL. I would like to random select records from a table. Everytime the query will execute the resultset need to be diferent (random selected records from the table). Anybody any idea how i could do this?

Greetz,
Rick

Spookster
01-16-2003, 05:37 PM
You don't want the same records selected more than once? That would have to be done mostly with a server-side language other than SQL.

You would first have to select all the records to get what records exist then put that into an array and randomly select elements from that array and remove them once they have been selected then build a query based off that.

Dylan Leblanc
01-16-2003, 08:40 PM
You could use ORDER BY RAND()

Spookster
01-16-2003, 10:04 PM
Originally posted by Dylan Leblanc
You could use ORDER BY RAND()

That allows the possibility of selecting the same records the next time the query is run though. I don't think that is what they want based off of what they said.

Dylan Leblanc
01-16-2003, 11:11 PM
Then they could do something liek this (depending on the field names):

SELECT * WHERE id NOT IN (x,y,z) ORDER BY RAND()