Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mySQL query: last record

Status
Not open for further replies.

disord3r

Programmer
Apr 12, 2002
189
US
I have a very simple table (columns are: stamp, name, text) and I need to pull the record that was most recently inserted to do a comparison with the name field.

Stamp, like it would suggest, is the date that each partiulcar row was added, but is simply text. In my haste to get this thing up and running, I opted not to try to figure out how to do a wacky conversion of the default time/date stamp from whatever format it ends up being (hh:mm:ss mm/dd/yy, I believe) to the special little format I wanted (hh.mm.ss.mm.dd.yy). As a result, I went with a text field, and I enter the date for each row that gets added.

I have no ID field, and I can't think of any other way to order them chronologically without some significant changes to the database and scripts, so I wonder if there's another way. I don't think I can use the order by function on any of these fields, as they're all text.

If I just do a "select *", will the be ordered in the recordset in the order that they were inserted? Would it possibly be as easy as selecting them all, and manually advancing the cursor past 'numrows-1' records? Or is there an easier way to advance the cursor directly to the last record?

If someone knows an easy way to have the date converted from a default date/time stamp format to my special little format (hh.mm.ss.mm.dd.yy), that'd be cool, and I may just decide to change that columns format and re-entering the few dates I have, but I just want to do what's easiest. :)
 
Option 1 post some code please.
how do you format the dat into your stamp field and what does it look like? (YYYY-MM-DD etc etc)?

I take it you post the dat from a PHP form ? lookup mktime and date() functions.

post some code :)



option 2 :
change the field from text to INT and set its auto_increment and unique attributes, then this would be childs play.

SELECT * from table where stamp = MAX(stamp); ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Karver about your option 2:

MAX(stamp) doesn't guaranty that you will get the record you just inserted, particularly in a busy web environment.

Functions similar to the MySQL function mysql_insert_id() or the PostgreSQL function pg_last_oid() are the only way to get the id of the last record inserted on a given script's connection. ______________________________________________________________________
TANSTAAFL!
 
True, but no-one mentioned a busy web environment .... yet :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Karver, you know better than that. If you program for a busy web environment from the get-go every time, you can only be pleasantly suprised. ______________________________________________________________________
TANSTAAFL!
 
/hides ... <-- programs for an unbusy (100 user) intranet environment when theres no-one more skilled to it .....

Call it a side-line, my main job is NOT web related :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
I refer you to Blackwell's Doctrine of Strategic Laziness, which states, &quot;It always takes fewer resources to do the job right the first time.&quot;

Any time you are in a multiuser environment, regardless of the user interface, program to maintain data integrity. I have seen more than database app die and trash data simply because the programmer didn't take into account that another user might alter the table while his program was doing something else. ______________________________________________________________________
TANSTAAFL!
 
I will NOT fight ... I will not fight ... I will no......

u get the picture :) ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top