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.
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.
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.
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.