SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    single query to insert and get last_id or get existing id?

    Hi,

    I thought I read a nifty solution from rudy a couple of weeks ago but I can't seem to find it.

    I want to insert a new record to a table and get the last_insert_id but in the same query, I want to get the existing id if the recrd was already there.


    here is how I currently insert and get last_insert_id

    Code MySQL:
    INSERT into file_sequencing
      ( business_id
      , content_category
      , file_name_in_home_language
      , last_updated
      , last_updated_by
      )
      values ( '123', 'pages', 'Home', CURRENT_TIMESTAMP, ? )

    and here is how I get last_insert_id

    Code MySQL:

    all done in a transaction.

    I want to get the file_id for this file_name, when there is a new record created or it already exists.

    Can it be done in one query?

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume by transaction, you mean a single PHP statement and not a DB transaction?

    You could do that using a stored procedure... or a mysql-i multi-query... but with straight PHP+MySQL, not possible in one statement, as far as I know.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,016
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    bazz, you're thinking of INSERT with ON DUPLICATE KEY, and of course the ON DUPLICATE KEY requires that you have defined a unique key in the table other than the auto_increment

    then getting the id whether the insert succeeded or not will depend on a second query which searches on the unique key
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    R937,

    Can you do an ON DUPLICATE KEY SELECT id?

    I've never seen that trigger used that way.

    Would be pretty slick though!!!

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,016
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by transio View Post
    Can you do an ON DUPLICATE KEY SELECT id?
    sadly, da manual he say no
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks.

    on dulpicate key would be ok except I wanted to get the id (or the last_insert_id), as appropriate to what was/was not, entered.

    I have done it with two queries now - insert and then queried for the id where conditions are true.

    bazz


Bookmarks

Posting Permissions

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