Last week I needed to write an update SQL script that would insert some records into a database. However as the script was to be incorporated into a software update that was going to be deployed to all our customers, it needed to check some condition first and see, whether the insert was required at all.
Even though MySQL provides some non-standard SQL enhancement
there is no INSERT IF EXISTS kind of statement.
I managed to do it using some temp-tables and a combination of
INSERT IGNORE ... SELECT FROM and UPDATE ...
WHERE statements.
The following example demonstrates how to insert a new row of
data in the table named real_table. The data must
only be inserted into this table, if another record in a table
named condition_table exists. No change of
real_table must occur, if there is no record
matching the condition.
Moreover (because I …
[Read more]