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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:53:51+00:00 2026-05-28T20:53:51+00:00

I have a MySQL (5.1.49) table with three columns. mysql> create table testme(id int

  • 0

I have a MySQL (5.1.49) table with three columns.

mysql> create table testme(id int auto_increment, id2 int not null, somedate datetime, primary key(id));

In my case id2 is not unique, but I want to return rows with distinct id2 values with the max somedate.

Here’s some sample data.

mysql> insert into testme values (1, 5, '2012-01-02 01:01:01'),(2, 5, '2012-02-02 02:02:02'),(3, 7, '2010-01-10 11:01:33');

This question almost answers mine, but with the extra id field, the returned id and id2 don’t match. For id2=5, it’s returning id=1 instead of id=2.

mysql> select id, id2, max(somedate) from testme group by id2;
+----+-----+---------------------+
| id | id2 | max(somedate)       |
+----+-----+---------------------+
|  1 |   5 | 2012-02-02 02:02:02 |
|  3 |   7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+

I’m expecting,

+----+-----+---------------------+
| id | id2 | max(somedate)       |
+----+-----+---------------------+
|  2 |   5 | 2012-02-02 02:02:02 |
|  3 |   7 | 2010-01-10 11:01:33 |
+----+-----+---------------------+

Want the ID that matches maximum date for each ID2

Does anyone have any ideas please? 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-05-28T20:53:51+00:00Added an answer on May 28, 2026 at 8:53 pm

    This query would definitely work, although it may be not the optimal one:

    select t.id, s.id2, s.somedate 
    from testme t 
      join 
        ( select id2, max(somedate) as somedate 
          from testme 
          group by id2
        ) s 
        on  s.id2 = t.id2 
        and s.somedate = t.somedate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this MySQL table: CREATE TABLE `triple` ( `id_one` int(11) NOT NULL, `id_two`
I have a table defined like this: CREATE TABLE mytable (id INT NOT NULL
I have multiple columns in a mySQL table. Three of the columns are named
If I have three columns in my MySQL table people , say id, name,
I have a MySQL table with the following columns: id(int), date (timestamp), starttime(varchar), endtime(varchar),
I have a primary key in my mysql table which is comprised of three
I have one MySQL table, users , with the following columns: user_id (PK) email
I have a table containing (essentially) three columns - id, name, ref_id. I would
I created 4 columns for a table cur.execute(CREATE TABLE videoinfo ( id INT UNSIGNED
I have a table that was created using this mysql statement below. CREATE TABLE

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.