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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:37:42+00:00 2026-06-12T03:37:42+00:00

I am rather new to MySQL, I have this problem, which I have searched

  • 0

I am rather new to MySQL, I have this problem, which I have searched for help with, but could not find the answer (possibly because I am not sure what to search for). I am sure there is a simple solution.

I have a simple MySQL table like this:

id   |   referenceid   |   status
40       104702            4
39       104720            2
38       104720            0
37       104719            2
36       104719            0
35       104702            2
34       104702            0
41       104719            5

I want to fetch an array that will produce the following output:

For each unique “referenceid” field, I want to know the highest “ID” (ie most recent) and its “Status”.

So I have tried this query:

$result = mysql_query(
  "SELECT status,id,referenceid 
   FROM image_status_tracking 
   GROUP BY referenceid",$db);

while ($row = mysql_fetch_array($result)) {
  $id = $row['id'];
  $status = $row['status'];
  $referenceid = $row['referenceid'];
  echo "ID $id, REFID $referenceid, Status $status <br />";
}

which gives this output:

ID 40, REFID 104702, Status 4

ID 37, REFID 104719, Status 2

ID 39, REFID 104720, Status 2 

But this is not the result I was hoping for. The ID and Status for referenceid 104719 should be 41 and 5, not 37 and 2.

I cannot work out how to get the most recent ID line output for each referenceid.

Something I noticed, in PHPMYADMIN, (and you can see in the table above), the IDs are listed in reverse order, with the latest ID at the bottom. I am not sure if that is relevant to this problem.

I hope this is clear, I am sure it is a simple matter.

Thank you for reading.

James

  • 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-12T03:37:43+00:00Added an answer on June 12, 2026 at 3:37 am

    Typing from memory so you might have to correct the syntax a little bit but the concept, i think, should work.

    select * 
      from image_status_tracking 
     where id in (
                  select max(id) from image_status_tracking 
                  GROUP BY referenceid
                 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm rather new to Django and I'm using Django 1.0. I have this: forms.py:
I have a problem that is common, but I am not quite sure exactly
Rather noobish question but I have tried searching and just cannot find a solution.
I'm about to start a new project which should have a rather large database.
This is probably a rather stupid problem but I am playing around with gridbag
Rather new to php, so sorry if this seems stupid. I'm really copying a
I'm rather new to jQuery, so I really don't know if the following problem
I'm rather new to Backbone.js development, and have run into a bit of a
I have asked aqbout timezones and date/time before but this is a more specific
I currently have the following MySQL query, which I would like to include in

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.