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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:46:09+00:00 2026-05-29T05:46:09+00:00

Let’s say that we have a table with COLUMN1 and COLUMN 2. Here’s a

  • 0

Let’s say that we have a table with COLUMN1 and COLUMN 2. Here’s a sample of the records:

COLUMN 1 | COLUMN 2
124 | 12
124 | 11
124 | 10
124 | 9
26  | 8
65  | 7
65  | 6
65  | 5
65  | 4
23  | 3
124 | 2
124 | 1
124 | 0

There is absolutely no pattern to this, but what I’d like to do is get:

COUNT(*) | COLUMN 1 | Smallest Column 2
4 | 124 | 9
1 | 26  | 8
4 | 65  | 4
1 | 23  | 3
3 | 124 | 0

So far, I’ve been doing this with PHP, but I’d like to find a way to do this in MySQL, as I’m sure it’d be a lot more efficient. The problem is, I can’t even think of where to start with this. A regular GROUP BY COLUMN 1 wouldn’t work because I want two results for 124, since it appears in two different instances. I’ve been fiddling around for hours and looking into the documentation and Google, but I haven’t been able to find anything yet, and I was wondering if any of you would be able to point me in the right direction. Is this even possible with MySQL?

  • 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-29T05:46:11+00:00Added an answer on May 29, 2026 at 5:46 am

    Well, it took a bit of fiddling, but here it is!

    This assumes you have an id column in your table that you order by to get a consistent ordering (if you don’t have an id column, order by timestamp or whatever in the inner query).

    set @prev := '', @low := 0, @cnt := 0, @grp :=0;
    select cnt, column1, low
    from (
        select
            column2, 
            @low := if(@prev = column1, least(column2, @low), column2) low,
            @cnt := if(@prev = column1, @cnt + 1, 1) cnt,
            @grp := if(@prev = column1, @grp, @grp + 1) grp,
            @prev := column1 column1
        from (select column1, column2 from so9091342 order by id) x
        order by grp, cnt desc) y
    group by grp;
    

    Here’s the sql needed to set up a table for testing:

    create table so9091342 (id int primary key auto_increment, column1 int, column2 int);
    insert into so9091342 (column1, column2) values (124,12),(124,11),(124,10),(124,9),(26,8),(65,7),(65,6),(65,5),(65,4),(23,3),(124,2),(124,1),(124,0);
    

    Output of above query:

    +------+---------+------+
    | cnt  | column1 | low  |
    +------+---------+------+
    |    4 |     124 |    9 |
    |    1 |      26 |    8 |
    |    4 |      65 |    4 |
    |    1 |      23 |    3 |
    |    3 |     124 |    0 |
    +------+---------+------+
    

    p.s. I named the table so9091342 because this is SO question ID #9091342.

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

Sidebar

Related Questions

Let's say I have table with column 'URL' whrere I store urls like this
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say that I have a set of relations that looks like this: relations
Let's say that I have a binary that I am building, and I include
Let's say that I have classes like this: public class Parent { public int
Let's say I'm creating an OpenGL game in C++ that will have many objects
Let's say I have this MySQL table: OK.. see the type field? Type 0

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.