Selecting the most recent entry for a person in MySQL

03 Jul 2004
Posted by joshb

Here's a snippet of code to get the most recent entry for a person from a list of records with dates. I use this in a situation where I have a list of GPAs for students. The list of GPAs is updated each day for a couple of weeks. The challenge is that some students graduate and are dropped from the list each day so I need to pick the most recent record for each person in the database.

SELECT name, gpa, standing, run_date
FROM `ranking` AS a
WHERE ( standing = '123' OR standing = '122' ) AND a.run_date = (
SELECT MAX( run_date )
FROM `ranking` AS b
WHERE b.name = a.name )
ORDER BY standing, gpa DESC

Tags:

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Link to Amazon products with: [amazon product_id inline|full|thumbnail]. Example: [amazon 1590597559 thumbnail]
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You may insert videos with [video:URL]
  • Use [fn]...[/fn] (or <fn>...</fn>) to insert automatically numbered footnotes.
  • Allowed HTML tags: <a> <b> <dd> <dl> <dt> <i> <li> <ol> <u> <ul><p> <img> <table> <tr> <td><strong><em><sup><div><fn><h1><h2><h3><h4><blockquote><img style="">
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically. (Better URL filter.)

More information about formatting options

 
 
 

Live from twitter...

  • How did I miss the coffee pot this morning? Drupal is better with coffee. 13 min 11 sec ago
  • Funny how much relief knowing that I have a meeting setup with a good attorney to handle specious claims is. 16 hours 11 min ago
  • @tacanderson Downtown Star! :) 21 hours 24 min ago
  • @seansaxon now what did Nebraska do to deserve that? 21 hours 24 min ago