tags:

views:

53

answers:

3

This is what I'm doing right now:

Execute a query and check if the date to be inserted already exists in a table.

If date doesn't exist:

Another query will insert the date into the table.

How can these two query be combined?

+3  A: 
IF NOT EXISTS (SELECT * FROM X WHERE A=B)
    INSERT INTO ...
František Žiačik
I tried this: IF NOT EXISTS(SELECT id FROM abc WHERE date='2010-06-02' and position='5') INSERT INTO abc (date, position) VALUES ('2010-06-02','5') but it's throwing some error in MYSQL
Yeti
Sorry, didn't notice it was for MySql. In that case, you may try this syntax: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.htmlIf date is not a primary key, you may try adding a unique index on that (with that logic it's unique anyway).
František Žiačik
A: 

Use a conditional statement.

Examples

Chathuranga Chandrasekara
A: 

you can put the two statements together into a stored procedure

Patrick Säuerl