Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4329264
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:48:02+00:00 2026-05-21T09:48:02+00:00

Pretty similar to MYSQL – select first 4 records for each category in a

  • 0

Pretty similar to MYSQL – select first 4 records for each category in a table but there isn’t an accepted answer and the one answer there doesn’t make much sense so i’m asking again.

I have a PRODUCTS table with 3 columns: ID, NAME and CATEGORY
What i would like to know now is if it’s at all possible to select 2 products for each distinct category without doing queries in a PHP loop.

The order of the selected products is of no importance, they might as well be random. But it’s important that i only have max 2 products per category.

So a good result set would be

ID  ; NAME   ; CATEGORY
:::::::::::::::::::::::
152 ; APPLE  ; FRUIT
185 ; ORANGE ; FRUIT
145 ; BEEF   ; MEAT
141 ; PORK   ; MEAT
410 ; PEPSI  ; DRINKS
585 ; CARROT ; VEGETABLES
585 ; TOMATO ; VEGETABLES
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:48:03+00:00Added an answer on May 21, 2026 at 9:48 am

    Something along these lines will work:

    SELECT id, name, category 
    FROM (
      SELECT *, 
             IF( @prev <> category, 
                 @rownum := 1, 
                 @rownum := @rownum+1 
             ) AS rank, 
             @prev := category, 
             @rownum  
      FROM (
        SELECT * FROM products 
        ORDER BY category, rand()
      ) random_prodcts
    ) products_ranked 
    WHERE rank <= 2;
    

    It orders them randomly within the categories, then pulls them out tracking how many it’s got from each.

    Not sure how nicely it will scale though.

    EDIT: Tried it with a few thousand records and it seems ok.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is pretty similar to that except but I need only the header
The javascript one seems pretty simple, just localStorage.clear(). Is there anything similar to that
I asked a pretty similar question a few days ago, but this one is
To be honest, I'm feeling pretty stupid right now. But this simply isn't working
I have a project with multiple targets each of which builds a pretty similar
My question is pretty similar to MySQL: is it possible to group_concat multiple rows?
There was a similar question asked here three years ago, but I want to
They seems pretty similar.What is the different? If I choose wrong, can I change
I've setup things in a pretty similar way to the blog posts about it
I was asked to create a Board Game AI(The game is pretty similar to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.