Page 1 of 1

how to perform a 'quiet' insert?

Posted: Wed Oct 06, 2004 10:35 am
by jasongr
Hello

I was wondering if it is possible to perform a 'quiet' insert in MySQL.
Here is what I mean by a 'quiet' insert.

if row with ID 1 already exists in a table, then its value should be updated using UPDATE TABLE, otherwise it should be added using INSERT INTO

It is possible to perform a SELECT to see if the row exists and then to do an UPADTE or INSERT based on the results but it seems like a non-efficient way to do it.

Is there a way to do it in a single command?
The command will update the table if the row already exists, or insert it otherwise

thanks

Posted: Wed Oct 06, 2004 10:39 am
by feyd
[mysql_man]REPLACE[/mysql_man]

Posted: Wed Oct 06, 2004 10:44 am
by jasongr
thanks
this is a very usefull command

Posted: Wed Oct 06, 2004 10:47 am
by Draco_03
Feyd is a man of few words..
But wise these words are

remeber

Posted: Wed Oct 06, 2004 11:11 am
by phpScott
just remeber last time a checke replace wasn't a standart sql command so portablity might be an issue if you plan to move your code.

Posted: Wed Oct 06, 2004 12:31 pm
by jasongr
I also found the following:

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},...),(...),...
[ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

Maybe it will be useful to some people