I want to run a query set to insert some data into an SQL table, but only if a record is fulfilled that meets certain criteria. The table has 4 fields: id (primary), fund_id , date and price
I have 3 fields in the request: fund_id , date and price .
So my query would look something like this:
INSERT INTO funds (fund_id, date, price) VALUES (23, '2013-02-12', 22.43) WHERE NOT EXISTS ( SELECT * FROM funds WHERE fund_id = 23 AND date = '2013-02-12' );
Therefore, I only want to insert data if the record corresponding to fund_id and date does not exist yet. If the above is true, this seems like a rather inefficient way to achieve this, since an additional select statement must be executed each time.
Is there a better way to achieve the above?
Edit: for clarification, neither fund_id nor date are unique fields; records that have the same fund_id or date file will exist, but no record should have the same fund_id or date as the other.
sql mysql sql-insert
harryg May 9 '13 at 11:12 2013-05-09 11:12
source share