Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2004
    Location
    California
    Posts
    10

    Unanswered: Last Insert Question

    I know that with Last_Insert_ID I can get the last inserted ID...but if the insert inserted more than one row, it only pulls the first id inserted within that group. How can I get all of the ID's inserted in that group?

  2. #2
    Join Date
    Oct 2003
    Location
    Denver, Colorado
    Posts
    137

    Re: Last Insert Question

    Do you always know how many rows you insert?

  3. #3
    Join Date
    Feb 2004
    Posts
    14
    Well, assuming you can guarantee that nobody will write to the table at the same time that you are, you could always count the number of elements you're inserting, fetch the ID of the first one that was inserted, and then simply correlate them.

    No idea on whether MySQL has transactions or any sort of normal locking though (I'm a PGSQL guy).

  4. #4
    Join Date
    Oct 2003
    Location
    Denver, Colorado
    Posts
    137

    Re: Last Insert Question

    Actually, you could do things this way:
    Code:
    LOCK TABLES inserttable WRITE;
    INSERT INTO inserttable... <your insert statement>;
    SELECT autoid FROM inserttable
    WHERE autoid >= LAST_INSERT_ID();
    UNLOCK TABLES;
    That will give you all the ids that were inserted. I know that MyISAM tables do not use ids missing from the middle of the table, so you are safe there. The LOCK TABLES statement will guarantee that nobody interferes with your insert.

  5. #5
    Join Date
    Jan 2004
    Location
    California
    Posts
    10
    Thanks aus!! Works perfect!

Posting Permissions

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