Choosing Random Items from a MySQL Table

January 2nd, 2010 admin

At one time or another, most of us find the need to display a random item from a batch of items on our Web site. Whether you are displaying a random banner or promotional item on your home page, or displaying a random post on your blog, you need to know how to do so effectively and efficiently. This article will show you two ways to do so. First, you can use the RAND() function within MySQL. To do that, you would simply use something like: SELECT * FROM table ORDER BY RAND() LIMIT 1; That will return a single random result from your table. That’s the simple way to select a random item from your table. Using the LIMIT part of that query, you could actually select…


Originally posted on html Center

 
  Related Posts
Finding the ID of a Newly Inserted MySQL Row
If you’re anything like me, you generally set up an auto-incrementing integer field in most of your MySQL tables that can be used as the primary key for your records. A lot of times, after inserting new information into the database, you need a way to find out what ID was assigned to your data when you created the new row. With MySQL, it’s... 
Copying Data from SQL Server to MySQL
In my last post, I showed you a way to copy the structure of an SQL Server table into a new MySQL table. In this post, I will show you how you can fairly easily copy the data from that SQL Server table into your new MySQL table. Before we start, we should make sure we have a complete list of the columns in our MySQL table (which …  Read More →
Free Performance with MySQL Table Types
Free Performance with MySQL Table Types
Last week, SitePoint launched its second Twitaway , giving away a free PDF of The Principles of Successful Freelancing to anyone kind enough to follow us on Twitter . Predictably, the site we built for readers to claim the PDF was swamped with traffic in the first few hours of the giveaway, and the MySQL server behind the scenes quickly ground... 
Using PHP to Copy an SQL Server Table to MySQL
At work, we have a scheduled task run every day that pulls information out of a huge Oracle database and dumps it into a Microsoft SQL table on one of our local IIS Web servers. We then have a script on our remote LAMPP Web server (the one we use to serve up our public Web site) that queries that MSSQL table and displays results on the Web site.... 
Top 20+ MySQL Best Practices
Top 20+ MySQL Best Practices
Database operations often tend to be the main bottleneck for most web applications today. It’s not only the DBA’s (database administrators) that have to worry about these performance issues. We as programmers need to do our part by structuring tables properly, writing optimized queries and better code. Here are some MySQL optimization... 
Find an Item in an Array (PHP)
On occasion, I’m sure you’ve found yourself in a position where you need to find an item inside of an array. PHP offers a very simple method to do just that. The array_search function lets you search for the item and then tells you what the index for that item is. This function works with numerically indexed arrays and with associative... 
Announcement: Kentico CMS License Winner
Announcement: Kentico CMS License Winner
Last week we hosted a giveaway by Kentico for a full license of Kentico CMS (valued at $1,999 ). There were over 80 participants eager to win this awesome prize by citing what feature of Kentico they were most interested in. Today we announce the Six Revisions reader that won this giveaway. The Winner KAL Join me in congratulating KAL! KAL will... 
From a Web Designer’s Point of View: Is the client leaving clients on the table?
I recently wrote an article here on woorkup.com entitled “ As a web designer, are you leaving money on the table? ”. It created quite a stir across twitter and other social networks. In the article, I communicated that once our firm left the standard hours/project based billing system and moved into a retained system, our revenues rose significantly,... 
Oracle Publicly Commits to MySQL
Oracle Publicly Commits to MySQL
Oracle’s plans for acquiring Sun have been on hold since April 2009. Although the US Department of Justice approved the deal, the European Commission formally objected . This week, MySQL creator Michael Widenius also put out an urgent call to save MySQL from Oracle’s clutches . His reasons: Rather than working with the EU, Oracle contacted... 
Quick Tip: Fully Understanding $.grep()
Quick Tip: Fully Understanding $.grep()
The jQuery method $.grep() is one of those methods that isn’t used as often as it should be. This is mostly because, until you understand exactly what it does, it can be a bit confusing. Hopefully, this video quick tip will explain when and why you would use it. At its core, $.grep is a simple little method that will filter through an array... 
  Related Tweets from Twitter
rsslance (RSS Lance)  : Web Developer PHP, JS, CSS, RSS, MySQL, AJAX (Miami): http://urluda.com/19ne..
Updated : 2010-03-11T17:33:52Z   |  Reply  |  View Tweet
block76 (block76)  : Exporting data from MYSQL to access made easy http://bit.ly/cRABTT..
Updated : 2010-03-11T17:33:33Z   |  Reply  |  View Tweet
alisohani (Ali Sohani)  : Twitter storing all tweets in #Cassandra (parallel to #mysql cluster). Current #nosql cluster: 45 nodes. #nosqllive @igrigorik..
Updated : 2010-03-11T17:32:06Z   |  Reply  |  View Tweet
news_itc_com (News ITC)  : Virtual Users And Domains With Postfix, Courier, MySQL And SquirrelMail (Mandriva 2010.0 x86_64) http://tinyurl.com/yz5xgzp..
Updated : 2010-03-11T17:30:09Z   |  Reply  |  View Tweet
esloof (Eric Sloof)  : RT @sakutz: updated blog post on #simdk Virtual Appliance with Perl, PowerShell, and MySQL examples: http://bit.ly/cMQW3L..
Updated : 2010-03-11T17:28:52Z   |  Reply  |  View Tweet
  Related News from Digg
No comments yet.

Spam Protection by WP-SpamFree

TOP