2006 Aug 29

Random MySQL Results

I never knew there was a built-in random function in MySQL. What i did was get all the rows and using PHP's array_rand to get a random set of rows. When using RAND() in MySQL together with the LIMIT keywords it returns only the number of rows required for a random. It really saved so many lines of my code.

We can get a random number from the MySQL server using:
mysql> SELECT RAND();

We can also add a seed value by adding an argument to our RAND function:
mysql> SELECT RAND(34);

To retrive rows in random we use:
mysql> SELECT * FROM tbl_name ORDER BY RAND();

Now with the LIMIT keyword added:
mysql> SELECT * FROM tbl_name ORDER BY RAND() LIMIT 1000;

0 TrackBacks

Listed below are links to blogs that reference this entry: Random MySQL Results.

TrackBack URL for this entry: http://www.tildemark.com/cgi-bin/mt4/mt-tb.cgi/11

Leave a comment

About this Entry

This page contains a single entry by tildemark published on August 29, 2006 4:02 PM.

What does it take to be a bowling champion? was the previous entry in this blog.

Adsense Section Targeting is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Recent Activity

Friday

  • tildemark tweeted, "im so sleepy. Zzzzzzzz"

Sunday

  • tildemark tweeted, "some of my scipts are not working with godaddy. but works fine on the others. not mention their poorly coded admin page"

Today

  • tildemark tweeted, "so many pending tasks i need to finish. need more coffee !!!"
  • tildemark tweeted, "@gmtristan i dont think that is true."

Today

  • tildemark tweeted, "how does godaddy subdomain behaves? i have some problems with it on my scripts. it does not seem to accept query strings.."

Monday

  • tildemark tweeted, "i had a hard time removing the error messages generated by surf side kick. i ended up uninstalling most of my applications."

Sunday

  • tildemark tweeted, "i got hit by surf side kick and im getting numerous error messages on my screen. tskkkkk"

Saturday

  • tildemark tweeted, "check boxes, i didn't know they can also be complex"
  • tildemark tweeted, "this smart bro internet speed is depressing, i thinking of filling a complaint to the DTI next week."

Friday

  • tildemark tweeted, "the seminar turned out to be leadership training. it was fun, learned alot. i have already attended numerous seminars but this is different."