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 4530882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T13:50:56+00:00 2026-05-21T13:50:56+00:00

I have 2 tables, one has the categories within it, and the other has

  • 0

I have 2 tables, one has the categories within it, and the other has the distinct items in it.

What I want to do is to select
the item_id and cat_id, and limit it to 5 result for each category, for example:

cat_id item_id
1      1
1      2
1      3
1      4
2      5
2      6
3      7

etc…
The closest query i’ve come up with

SELECT cat.cat_id, cat.cat_name, item_id, item_author, item_name, item_pic_big
FROM item_table ipt
JOIN cat_table cat ON ipt.cat_id = cat.cat_id
GROUP BY item_id
HAVING COUNT(*) < 5
ORDER BY cat.cat_id

That’s what I found on stackoverflow, however if I want to change the count to let’s say… 2, it gives me the same result, and if I change the group by to cat_id, it only gives me 1 result.

Any help would be appreciated

  • 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-21T13:50:56+00:00Added an answer on May 21, 2026 at 1:50 pm

    What you actually need is something like:

    SELECT
    cat.cat_id, cat.cat_name, item_id, item_author, item_name, item_pic_big
    FROM item_table a
    JOIN cat_table b ON a.cat_id = b.cat_id
    WHERE a.item_id IN (SELECT item_id 
                        FROM item_table 
                        WHERE cat_id = b.cat_id 
                        LIMIT 5)
    GROUP BY a.item_id
    ORDER BY b.cat_id
    

    Unfortunately, if you try to run this you will get this disappointing
    error message:

    This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
    

    So you’ll need a workaround. You can see a list of possible workarounds here:
    http://www.artfulsoftware.com/infotree/queries.php#104

    EDIT: The first solution there can be translated to your structure like this:
    (I don’t have your tables, so there may be minor column names issues)

    SELECT temp2.cat_id, temp2.item_id,
           temp2.cat_name, temp2.item_author, 
           temp2.item_name, temp2.item_pic_big
    FROM
        (SELECT
              temp.cat_id,
              temp.item_id,
              temp.cat_name,
              temp.item_author,
              temp.item_name,
              temp.item_pic_big,
              IF( @prev <> temp.cat_id, @rownum := 1, @rownum := @rownum+1 ) AS rank,
              @prev := temp.cat_id
        FROM (SELECT
             a.item_id,
             b.cat_id,
             b.cat_name,
             a.item_author,
             a.item_name,
             a.item_pic_big
             FROM item_table a
             JOIN cat_table b ON a.cat_id = b.cat_id
             ORDER BY cat_id, item_id) AS temp
        JOIN (SELECT @rownum := NULL, @prev := 0) AS r
        ORDER BY temp.cat_id, temp.item_id) as temp2
    WHERE temp2.rank <= 5
    ORDER BY temp2.cat_id, temp2.item_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables. One has some product details, the other holds various photos
I have two tables, one that has a foreign key from the other. I
I have a table which has several one to many relationships with other tables.
I have two tables, one with categories and subcategories. Each category and subcategory has
Suppose I have two tables that are linked (one has a foreign key to
I have two tables. One (Widgets) has a list of widgets (ID, widget_name, color,
i have two data tables with the same structure the first one has one
I have a tables Foo and Bar . Foo has one Bar . When
I have two tables, one is a table of forum threads. It has a
I have a dataset with multiple tables. One table in particular has one record

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.