MySQL if exist update else insert | prosoxi.com

Categorized | MySQL, PHP

MySQL if exist update else insert

I whant to write single sql for this:
if not exists (table colum value=something) insert new row else update table colum value
i know that MSSQL has this method, but how to do this in mysql?

cdl_capture_2011-09-29-35_ 000

Sources

http://forums.digitalpoint.com/showthread.php?t=311008

http://stackoverflow.com/questions/1361340/how-to-insert-if-not-exists-in-mysql

http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html


use INSERT IGNORE INTO table

see http://bogdan.org.ua/2007/10/18/mysql-insert-if-not-exists-syntax.html

there’s also INSERT … ON DUPLICATE KEY UPDATE syntax, you can find explanations ondev.mysql.com

or

$result = mysql_query("update test set col='test' where col_id='1';");		 
if (mysql_affected_rows()==0) {
	$result = mysql_query("insert into test (col_id, col) values ('1','test');");
}


Leave a Reply

 

Advertise

Whocallsme.gr Widget Lookup