Select only one random item from each category in MySQL

This tutorial shows how to select items randomly from each category in MySQL. So here we will select exactly one item randomly from each category. People who need sometimes to select photo from each photo category and display them on the page for animation purpose will be benefited.
In this example I have used table name starts with item. You can change the name and column names as per your needs and you will get the same results.

Final Output:

random item selection for each category in mysql

First create item category table which holds the category for items


Create table items which holds items from each category above


Insert dummy values to item_categories table


Insert dummy values to items table


Execute the below query


That’s all. Thanks for your reading. Please do not hesitate to ask any question by leaving a comment.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Twitter Facebook  Google Plus Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *