Results 1 to 4 of 4

Thread: Check for value before Insert

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Check for value before Insert

    Hi again,

    Using MySQL 5.0.

    Say I have a have a simple table with only two fields: the unique auto-incrementing ID and the value.

    Can I do this in just a SQL statement?
    1) Insert a new value into the table.
    2) If the value exists, then just give me the ID so I can use it in my other insert.
    3) Otherwise really insert the value and give me the ID.

    I've got #1 and #3.
    Is it possible to do #2 just in SQL or am I going to have to do a Select statement on the value, check to see if I get results in the language of my choice and then proceed accordingly?

    Thanks again.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Check for value before Insert

    Quote Originally Posted by nmadd
    or am I going to have to do a Select statement on the value, check to see if I get results in the language of my choice and then proceed accordingly?
    I've been waiting to see if anyone would pop in with something because this is what I've always done.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Check for value before Insert

    That's pretty much all you can do... tho if MySQL 5.0 supports stored procedures, it would be more efficient to put all 3 steps into one (as there aren't two separate trips to the server), and call that from your code.

  4. #4

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Check for value before Insert

    Thanks for the reply si.

    MySQL 5 does support stored procedures, however, my host only offers the old MySQL extension for PHP that does not support stored procedures. Oh well, what do I expect for $3.99/month.

    Time for lots of trips to the DB.

Posting Permissions

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



Click Here to Expand Forum to Full Width