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

  • SEARCH
  • Home
  • 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 9244991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:09:51+00:00 2026-06-18T09:09:51+00:00

I have a table product_category , with fields id , name , parent_id and

  • 0

I have a table product_category, with fields id, name, parent_id and level.

For example, the category Agriculture has id = 75, level = 1 and parent_id = NULL. The subcategories Corn, Wheat, Rye, etc. have level = 2 and parent_id = 75.

In my site I’d like to show the top-level categories and below each of them, only 5 subcategories. But doing a query to retrieve them it’s harder than what I thought.

If I do the following query:

SELECT a.name, a.parent_id FROM product_category a
WHERE (
    SELECT b.level
    FROM product_category b
    WHERE b.id = a.parent_id
    LIMIT 1
) = 1

I retrieve all top-level categories and subcategories, but there are thousands of subcategories so it would be very unnecessarily expensive when I only want the first 5 of each.

If I do the following one:

SELECT a.name, a.parent_id FROM product_category a
WHERE (
    SELECT b.level
    FROM product_category b
    WHERE b.id = a.parent_id
    LIMIT 1
) = 1
LIMIT 5

It only retrieves 5 subcategories, not 5 subcategories per top-level category.

Then I thought of doing it the following way:

(
     SELECT a.name, a.parent_id FROM product_category a
     WHERE parent_id = 12
     LIMIT 5
) UNION (
     SELECT a.name, a.parent_id FROM product_category a
     WHERE parent_id = 21
     LIMIT 5
) UNION (     
     SELECT a.name, a.parent_id FROM product_category a
     WHERE parent_id = 75
     LIMIT 5
) UNION (
.
.
.

Which looks very dirty and hard-coded, yet it is the only way I can think of right now. Is there any other solution to this?

Thanks!

  • 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-06-18T09:09:53+00:00Added an answer on June 18, 2026 at 9:09 am

    Here’s an example to return up to two subcategories per root category:

    select  parent.name as Category
    ,       child.name as SubCategory
    from    (
            select  name
            ,       parent_id
            ,       @rn := if(@cur = parent_id, @rn+1, 1) as rn
            ,       @cur := parent_id
            from    product_category pc
            join    (select @rn := 0, @cur := '') i
            where   level = 2
            order by
                    parent_id
            ,       id
            ) as child
    join    product_category as parent
    on      child.parent_id = parent.id
    where   child.rn < 3
    

    Live example at SQL Fiddle.

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

Sidebar

Related Questions

I have a Category table which has 3 fields i'm interested in: ID, Name,
I have product_details table which belongs to categories. product_details has fields like id, name,
I have a table [Product] with a column [CreateTime] datetime null, and is has
I have a products table, with the fields product, category and cost, of type
I have a Products table and Downloads table. The Downloads table has 4 fields,
I have two tables: products products_tmp The products_tmp table has less fields. But all
I have a table it has four fields : Phone_number(primary key),City_name,Category_product and sub_Category I
I have a table with just product ID's and category ID's (products can be
I have a 2 Table. Product ProductName CategoryID Category ID CategoryName I'm filling combobox
My prob in brief: I have two tables namely category and product. table: category

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.