Find the answer to your Linux question:
Results 1 to 4 of 4
i am new to MYSQL, so forgive the stupidity. actually worked with it years ago, but forgot most. anyway, i have a table, and the UserID column auto increments. and ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2004
    Posts
    4

    selecting last record


    i am new to MYSQL, so forgive the stupidity. actually worked with it years ago, but forgot most.

    anyway, i have a table, and the UserID column auto increments.

    and of course the only index i have is on THAT column.

    so AFTER inserting a NEW record, i want to go to the last RECORD and retireve that UserID as fast as possible.

    i know the whole "select UserID from TABLE"

    but how do i say WHERE LAST RECORD (google returns too many hits, can't find the right one)

    and do i need to specify the index?

    thanks

  2. #2
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    The function LAST_INSERT_ID() will return the last auto-incremented value. You don't even have to run a select statement. Just assign a variable to the value that LAST_INSERT_ID() returns.

    For future reference, mysql has a great documentation section at http://dev.mysql.com/doc
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  3. #3
    Just Joined!
    Join Date
    Oct 2004
    Posts
    4

    LAST_INSERT_ID()

    LAST_INSERT_ID()

    according to the dox, that's per client....i need the LAST # period, no matter who made it....MANY clients here

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Oct 2004
    Posts
    4

    how's this

    right now am doing this:

    SELECT UserID from MyTable order by UserID DESC limit 1;

    now the ONLY index that exists on this table is on UserID....do i need to include it somehow in that statement?

    thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •