List:General Discussion« Previous MessageNext Message »
From:Fred Read Date:April 29 1999 9:43am
Subject:Select most recent row - another one for the FAQ perhaps?
View as plain text  
I'm sure this one has been asked before but I can't find it in the
archives.

    "How do I return the most recent row for a given xxx?"

+-----------------+---------------+------+-----+---------------------+
| Field           | Type          | Null | Key | Default             |
+-----------------+---------------+------+-----+---------------------+
| Created         | datetime      |      |     | 0000-00-00 00:00:00 |
| UserID          | int(11)       |      | PRI | 0  (auto increment) | 
| UserName        | char(64)      | YES  |     | NULL                |
+-----------------+---------------+------+-----+---------------------+

My gut feeling is this *should* work, but it doesn't, it always returns 
"Empty set (0.08 sec)"...

    select 
        UserName, Created, max(Created) as Latest 
    from 
        MyTable
    where 
        UserName = 'MyUserName' 
    group by 
        UserName 
    having 
        Created = Latest;

I should know this but keep coming up blank, your help would be 
most appreciated...
-- 

If it ain't opinionated, it ain't Rich Teer.
Thread
Select most recent row - another one for the FAQ perhaps?Fred Read29 Apr
  • Re: Select most recent row - another one for the FAQ perhaps?Robin Bowes29 Apr
  • Re: Select most recent row - another one for the FAQ perhaps?Fred Read29 Apr
  • Re: Select most recent row - another one for the FAQ perhaps?Robin Bowes29 Apr
  • Re: Select most recent row - another one for the FAQ perhaps?Christian Mack29 Apr
    • Re: Select most recent row - another one for the FAQ perhaps?Fraser MacKenzie29 Apr
      • Re: Select most recent row - another one for the FAQ perhaps?Fred Read29 Apr
        • Select most recent row - another one for the FAQ perhaps?Fraser MacKenzie29 Apr
    • Re: Select most recent row - another one for the FAQ perhaps?Erik Liljencrantz29 Apr
  • Re: Select most recent row - another one for the FAQ perhaps?Fred Read29 Apr
    • Re: Select most recent row - another one for the FAQ perhaps?Michael Longval1 May
      • Re: Select most recent row - another one for the FAQ perhaps?Thimble Smith1 May
  • Re: Select most recent row - another one for the FAQ perhaps?Robin Bowes29 Apr
    • Re: Select most recent row - another one for the FAQ perhaps?Erik Liljencrantz29 Apr
      • Re: Select most recent row - another one for the FAQ perhaps?Michael Widenius30 Apr