• Home
  • About

szehau's weblog

Life, Internet, Software, Gadgets, Programming and Investments

  • life log
  • internet
  • programming
  • software
  • working holiday
  • gadget
  • project

MySQL randomly sort query result

July 25, 2005 23:23 by Sze Hau 2 Comments

How do you choose records randomly from MySQL database. Normally we will select all the related data from database and then store it to a buffer array. From the array, we randomly choose the records that we want.

There is a simpler way. Let say we have the following query which return 100 rows of record and we want to randomly choose 5 rows from it:

select column1, column2 from table1 where condition1 and condition2

We can simply tweak the query to return 5 rows randomly.

select column1, column2, rand () as randomColumn from table1 where condition1 and condition2 order by randomColumn limit 5

That’s it! We simply add a column with random values and the sorted by the randomColumn. This method is pretty useful for application such as Rotating Ads, Random Links/Post and etc.

Note: Do not use this method on query that return large amount of rows (e.g. more than 1 millions rows), it will slow down the overall execution time.

More from my site

  • www.szehau.com listed in Google search engine within one daywww.szehau.com listed in Google search engine within one day
  • Make deleted file unrecoverable by undelete softwareMake deleted file unrecoverable by undelete software
  • Firefox 1.0.5Firefox 1.0.5
  • ChartNexus – A Must Have Software for Beginner in Stock MarketChartNexus – A Must Have Software for Beginner in Stock Market
  • Another WordPress ThemeAnother WordPress Theme
  • Batu Cave and Pulau KetamBatu Cave and Pulau Ketam

Filed Under: programming

About Sze Hau

Geek. Love programming. Coffee addicted. Married with a child. Working towards financial freedom.

Comments

  1. EngLee says

    July 31, 2005 23:41 at 11:41 pm

    Hmmm.. learned a new thing! :)

    Reply

Trackbacks

  1. Java: Randomly sort values in a array (the generic way) in single pass - Snippet IT says:
    April 12, 2009 00:14 at 12:14 am

    […] years back (yes, about 4 years back), I wrote an article about randomly sort query results in MySQL. The main problem in randomly sort query results is that when you have a very large data in your […]

    Reply

Leave a Reply to Java: Randomly sort values in a array (the generic way) in single pass - Snippet IT Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

  • Facebook
  • Google+
  • Instagram
  • Twitter

More Posts

5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic

June 1, 2021 22:15 By Sze Hau Leave a Comment

LED Downlight

Troubleshooting A Flickering LED Downlight

February 13, 2016 00:29 By Sze Hau Leave a Comment

Repair A Broken iPhone Lightning Cable

January 30, 2016 00:54 By Sze Hau Leave a Comment

Sponsored Links

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • Snippet IT Information technology news, ideas and experiences and opinions

szehau’s weblog

szehau is a software programmer that has strong interest in the world of programming and Internet. He is graduated in University of Malaya, Malaysia with a Bachelor’s degree in Software Engineering. He draws on his passsion for web programming to share with others how to build a successful websites.

Recent

  • 5 Essential Things You Need To Have At Your Home During The COVID-19 Pandemic
  • Troubleshooting A Flickering LED Downlight
  • Repair A Broken iPhone Lightning Cable
  • New Gadget – MacBook Pro with Retina Display (2015)
  • One Watt LED Night Light

Search

Tags

adsense advertisement baby blog blogtal Canon EOS 400D Chinese New Year Christchurch CNY DiGi electronic Facebook FBM Flickr Flickr Uploadr Google Google Maps Google Search Results Google Webmaster Central H1N1 income tax investment joke KLCI KLSE Lake Tekapo Maxis Maybank2u Motueka movie mutual funds New Zealand PC Fair 2009 Queenstown restaurant for dinner sharing stock trackback travel Twitter unit trust visa wordpress wordpress plugin working holiday

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in