If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > get entry with the latest date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-09-03, 16:57
itsallgood23 itsallgood23 is offline
Registered User
 
Join Date: Sep 2003
Posts: 4
get entry with the latest date

I have a table that look like follows

TABLE: History
{
history_item_id (autonumber)
id (primary key)
label
street name
street num
old street name
old street num
lastupdated
}

The table is basically used to keep a log of all changes made to another table i use. I wanted to run a query that would generate the latest record for each id... if my table looked like the one below for eg"

34|1|test1|john street|23|john street|45|2003-08-03
35|1|test1|john street|56|john street|23|2003-08-05
36|1|test1|john street|523|john street|56|2003-08-05
37|2|test2|water street|1|water street|2|2003-08-05
38|2|test2|water street|11|water street|1|2003-08-05
39|2|test2|water street|12|water street|11|2003-08-05

the query should return:
36|1|test1|john street|523|john street|56|2003-08-05
39|2|test2|water street|12|water street|11|2003-08-05

If someone could help me out i would be very grateful
Thanks in advance !!!
Reply With Quote
  #2 (permalink)  
Old 09-09-03, 18:52
jbroder jbroder is offline
Registered User
 
Join Date: Jun 2003
Posts: 20
the example is hard to understand. the data is mostly the same.

I am not sure what you are asking. Can you make it more clear?
Reply With Quote
  #3 (permalink)  
Old 09-10-03, 02:20
asherh asherh is offline
Registered User
 
Join Date: Jul 2003
Posts: 34
Hi,

Just to clarify... are you trying to select the most recent entry by each user? If this is the case, you could try the following...

DROP TABLE IF EXISTS $myTable;
CREATE TEMPORARY TABLE $myTable (SELECT * FROM history ORDER BY userId, history_item_id DESC);
SELECT * FROM $myTable GROUP BY id;

Not sure if you can do this in one select statement (without subselects), as mysql can't order within groups... only the returned group values themselves... which would mean you keep getting the first sequenced record for each grouped user. Someone may believe this is not the case... anyone wish to clarify?

I have used the history_item_id for sorting, which assumes it is unique and sequential, as the lastUpdated values you have seem to be of type 'date'. I would probably change your lastUpdated column type to 'datetime' and insert a time component with each date... then change the history_item_id value in the "CREATE TEMP..." statement above to lastUpdated... so you are truely ordering by the date value.

Chrs,
Ash

Last edited by asherh; 09-10-03 at 02:29.
Reply With Quote
  #4 (permalink)  
Old 09-10-03, 07:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On