Jump to content

Mysql Rand Function


shah_ankitb
 Share

Recommended Posts

Hello Friends, In the MySQL we have RAND() function. Suppose i have 100 data in one table using this query alsoselect * from test ORDER BY rand()*123456789 ASC LIMIT 4 at some point i'm getting REPEATED data. So how can i get UNIQUE data without any repetation.

Link to comment
Share on other sites

using a loop, you could assign each result to an element in an array, and then check for each new rand() (past the first one) to see if its already in the array, and if it is, get another rand, else add it.

Link to comment
Share on other sites

First, there's no reason to multiply by a constant, that's just adding work for the database. Second, every time you run that query it will return a random 4 records. If you are running this query several times and always want different results, you'll need to keep track of which ones you've already seen and tell it not to return those, or keep getting one random row and checking if it was already seen before printing it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...