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... 
How to Create a PHP/MySQL Powered Forum from Scratch
How to Create a PHP/MySQL Powered Forum from Scratch
In this tutorial, we’re going to build a PHP/MySQL powered forum from scratch. This tutorial is perfect for getting used to basic PHP and database usage. Let’s dive right in! Step 1 : Creating Database Tables It’s always a good idea to start with creating a good data model when building an application. Let’s describe our... 
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... 
  Related Tweets from Twitter
cmartinez (Carlos Martnez U.)  : RT @uvmbdt: Urge programador que domine MySQL, enva tu CV a fosnaya@uvmnet.edu..
Updated : 2010-03-19T02:40:28Z   |  Reply  |  View Tweet
ronin4rent (Antonio Frye, Jr.)  : Twitter Drops MySQL For Cassandra -- Cloud databases -- InformationWeek: http://bit.ly/bk0sss via @addthis..
Updated : 2010-03-19T02:38:44Z   |  Reply  |  View Tweet
RKajiyama (Ryusuke Kajiyama)  : MySQL????????Oracle????REDO????????????? RT @knaka ?????????????????Oracle???REDO????PostgreSQL???WAL??????????..
Updated : 2010-03-19T02:37:35Z   |  Reply  |  View Tweet
sadamumu (???)  : mysql> select user, host, password from mysql.user;..
Updated : 2010-03-19T02:36:38Z   |  Reply  |  View Tweet
RKajiyama (Ryusuke Kajiyama)  : MySQL?????????????????????????????????General Log???????????????????????????????????InnoDB?REDO????????InnoDB??? RT @knaka ..
Updated : 2010-03-19T02:36:09Z   |  Reply  |  View Tweet
  Related News from Digg
No comments yet.

Spam Protection by WP-SpamFree

TOP