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... 
Profiling MySQL Queries with phpMyAdmin: New Premium Tutorial
Profiling MySQL Queries with phpMyAdmin: New Premium Tutorial
I’ve used phpMyAdmin for over a decade. In my early years with the tool, I simply needed something that could show me table structure and quickly give me the data inside. As my needs have grown, so have the tools included with phpMyAdmin which keeps me coming back as my primary MySQL tool, even with optimization. Become a Premium member.... 
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... 
Visual Database Creation with MySQL Workbench
Visual Database Creation with MySQL Workbench
In today’s tutorial, you’ll learn how to use a visual database modeling utility to draw a database diagram and automatically generate SQL. Specifically, we’ll review how to use MySQL Workbench , a cross-platform, visual database design tool. What is MySQL Workbench? MySQL Workbench is a powerful tool developed by MySQL with three... 
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,... 
  Related Tweets from Twitter
orj (Oliver Jones)  : Just got an email from "Oracle MySQL". Makes me shudder...
Updated : 2010-09-04T03:33:30Z   |  Reply  |  View Tweet
rmonteroo (Rob Montero)  : @Gorileo Estaba estudiando Oracle, segundo ao de carrera y preguntndome por qu no usaba eso con ASP en vez de Access. Viva PHP y mySQL...
Updated : 2010-09-04T03:30:30Z   |  Reply  |  View Tweet
ayatorch (???)  : TIG?DBLogger?MySQL?30???????????????????????1.84?????..
Updated : 2010-09-04T03:26:25Z   |  Reply  |  View Tweet
nbashaw (Nathan Bashaw)  : @emilyJbro just a simple form thing. I'm reading Head First PHP & MySQL by O'Reilly..
Updated : 2010-09-04T03:23:21Z   |  Reply  |  View Tweet
brendam (Brenda Moon)  : upgrading mysql from 5.1.40 to 5.1.50 has fixed not starting at boot, but seems to have lost my databases. oops...
Updated : 2010-09-04T03:21:19Z   |  Reply  |  View Tweet
  Related News from Digg
No comments yet.

Spam Protection by WP-SpamFree

TOP