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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:49:12+00:00 2026-05-29T09:49:12+00:00

I am using mysql. I don’t care how many groups get returned back but

  • 0

I am using mysql. I don’t care how many groups get returned back but if a single group has more then 4 items i only want the first 4. How do i write a statement that only returns 4rows per group? As a temporary fix i am just returning them all and filtering it out in code. Its still pretty fast although it would be easier if i knew syntax

  • 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-29T09:49:12+00:00Added an answer on May 29, 2026 at 9:49 am

    If I understand your question correctly, I believe the following answer should do approximately what you need (note: I’ve included two test tables and their associated inserts as an example since your table structure was not provided):

    Given these tables and data:

    DROP TABLE IF EXISTS tCustomer;
    CREATE TABLE tCustomer (
         customerId INT(11) UNSIGNED NOT NULL auto_increment,
         name       VARCHAR(8),
         PRIMARY KEY (customerId)
    ) AUTO_INCREMENT=1;
    
    INSERT INTO tCustomer VALUES
    (NULL, 'Alex'),
    (NULL, 'Bob'),
    (NULL, 'Carl')
    ;
    
    DROP TABLE IF EXISTS tPurchases;
    CREATE TABLE tPurchases (
        purchaseId   INT(11) UNSIGNED NOT NULL auto_increment,
        customerId   INT(11) UNSIGNED NOT NULL,
        amount       DECIMAL(9,2),
        purchaseDate DATETIME,
        PRIMARY KEY (purchaseId),
        CONSTRAINT fk_customer FOREIGN KEY (customerId) REFERENCES tCustomer (customerId)
    ) AUTO_INCREMENT=1;
    
    INSERT INTO tPurchases VALUES
    (NULL, 1, 1.00, '2011-01-01 08:00'),
    (NULL, 1, 1.01, '2011-01-02 08:00'),
    (NULL, 1, 1.02, '2011-01-03 08:00'),
    (NULL, 1, 1.03, '2011-01-04 08:00'),
    (NULL, 1, 1.04, '2011-01-05 08:00'),
    (NULL, 1, 1.05, '2011-01-06 08:00'),
    (NULL, 2, 1.01, '2011-01-01 08:00'),
    (NULL, 2, 1.02, '2011-01-02 08:00'),
    (NULL, 3, 1.01, '2011-01-02 08:00'),
    (NULL, 3, 1.02, '2011-01-04 08:00'),
    (NULL, 3, 1.03, '2011-01-08 08:00')
    ;
    

    The following SQL will select purchase data by customer returning no more than the 4 most recent purchases:

    SELECT
        tC.customerId, tC.name, tP.purchaseDate, tP.amount, COUNT(tPNewer.customerId) newerCt
    FROM
                        tCustomer  tC
        INNER JOIN      tPurchases tP      ON tC.customerId = tP.customerId
        LEFT OUTER JOIN tPurchases tPNewer ON tP.customerId = tPNewer.customerId AND tPNewer.purchaseId > tP.purchaseId
    GROUP BY
        tC.customerId,
        tC.name,
         tP.purchaseDate,
         tP.amount
    HAVING
        newerCt < 4 -- Ignore rows that have more than 3 newer records
    ORDER BY
         tC.customerId,
         tP.purchaseDate desc
    ;
    

    Here is the resulting output from this select (note that Alex’s oldest transactions are absent):

    +------------+------+---------------------+--------+---------+
    | customerId | name | purchaseDate        | amount | newerCt |
    +------------+------+---------------------+--------+---------+
    |          1 | Alex | 2011-01-06 08:00:00 |   1.05 |       0 |
    |          1 | Alex | 2011-01-05 08:00:00 |   1.04 |       1 |
    |          1 | Alex | 2011-01-04 08:00:00 |   1.03 |       2 |
    |          1 | Alex | 2011-01-03 08:00:00 |   1.02 |       3 |
    |          2 | Bob  | 2011-01-02 08:00:00 |   1.02 |       0 |
    |          2 | Bob  | 2011-01-01 08:00:00 |   1.01 |       1 |
    |          3 | Carl | 2011-01-08 08:00:00 |   1.03 |       0 |
    |          3 | Carl | 2011-01-04 08:00:00 |   1.02 |       1 |
    |          3 | Carl | 2011-01-02 08:00:00 |   1.01 |       2 |
    +------------+------+---------------------+--------+---------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using MySql in my asp.net project. But I don't want to type every
I'm using MySQL, but I think this is a basic SQL question. I don't
I've just solved an interesting problem using MySQL's IN, but I don't know what
I have been using MySQL for 2 years now, yet I still don't know
I'm using MySQL in particular, but I'm hoping for a cross-vendor solution. I'm using
This question was originally using MySQL 5.1.44, but is applicable to MySQL 8.0+ too.
The size of MySQL ibdata is 4GB, but I don't think the data I
We have a web application that has been built using MySQL / PHP /
I have been using mysql in a new rails application, but now I wanted
I'm using MySQL with Memcached, but I'm planning to start using PostgreSQL instead of

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.