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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:33:54+00:00 2026-06-11T23:33:54+00:00

Before I explain my problem, I will quickly go over how the table structure

  • 0

Before I explain my problem, I will quickly go over how the table structure is:

Type: MySQL
Posts/topic Table:

 int      int**     UNIX Time     Int        Int
--------------------------------------------------
| id | category | postdate | topic_id | is_topic |
--------------------------------------------------
|  1 |    a     | 12345678 |    1     |     1    |
--------------------------------------------------
|  2 |    a     | 12345678 |    1     |     0    |
--------------------------------------------------
|  3 |    b     | 12345678 |    3     |     1    |
--------------------------------------------------
|  4 |    b     | 12345678 |    3     |     0    |
--------------------------------------------------
|  5 |    c     | 12345678 |    5     |     1    |
--------------------------------------------------
|  6 |    c     | 12345678 |    5     |     0    |
--------------------------------------------------

**I’m using letters to make is easier to read the table

I am trying to retrieve the 4 newest rows for each category, and I am able to get the 1 newest from each category with GROUP BY, but I have no idea how to get multiple for each category.

I have tried something like this:

SELECT *
FROM posts p
WHERE NOT EXISTS
(
    SELECT *
    FROM posts
    WHERE category = p.category
    LIMIT 4
)

I also tried working with some of the other answers people supplied for some other answers here on SO, but I did not seem to be able to make them fit my purpose.

I am completely lost here, as mysql is not a strong side of mine when it comes to these more complex queries.

Any help or pointers in the right directions would really be appreciated!

Thanks!

UPDATE: Please note that the number of categories is not be static, and will change.

  • 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-11T23:33:55+00:00Added an answer on June 11, 2026 at 11:33 pm

    Something like this would probably do :

    select * from
        (select 
             @rank:=CASE WHEN @ranked <> category THEN 1 ELSE @rank+1 END as rank,
             id,
             category,
             postdate,
             topic_id,
             is_topic
             @ranked:=category
         from 
              (select @rank := -1) a, 
              (select @ranked :=- -1) b, 
              (select * from posts order by category, postdate desc) c
        ) ranked_posts
    where ranked_posts.rank <= 4
    

    Basically what is happening here is that I am trying to create a “ranking” function present in other engines (MS SQL comes in mind).

    The query goes through all the posts, ordered by category, postdate and adds a “ranking” number to every row resetting the rank when the category changes. Like:

    rank | category  
    1          a     
    2          a     
    ...
    100        a     
    1          b 
    2          b
    1          c
    2          c
    3          c
    

    You do that inside a “sub query” to mimic a table then just select the rows that have rank <= 4, (the ones you need). If you need more or less you can adjust that number.

    One thing to keep in mind is that the ordering is important or the ranks will get all screwed up. The categories have to be “grouped”, hence the ORDER BY category and then the groups ordered by your criteria postdate desc.

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

Sidebar

Related Questions

I have a table in postgresql. The following table animals will do to explain
First I will explain the context of the problem, because you might be able
I will explain my problem(excuse my bad English), I have a .NET exe in
I'm having a mysqli data fetching problem. I will try to explain my problem
i will try to explain the problem very clear. I user MicroSoftReportViewer where i
Ok, to you understand I will explain the problem: I am using a library
Let me just explain some definition before going to the problem: Say point A
Before I explain, here's the example HTML code : [ Demo on jsFiddle here.
I have this following jquery text fly-in animation.Here is my code before I explain
EDIT: You can see the search box live here . Before I explain, let

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.