unrelativity unrelativity - 1 month ago 13
MySQL Question

Create if an entry doesn't exist, otherwise update?

Kinda strange to put it into words that short, heh.

Anyway, what I want is basically to update an entry in a table if it does exist, otherwise to create a new one filling it with the same data.

I know that's easy, but I'm relatively new to MySQL in terms of how much I've used it :P

Answer

Use 'REPLACE INTO':

 REPLACE INTO table SET id = 42, foo = 'bar';

See more in the MySQL documentation