i trying insert multiple rows @ same time , check if exist. want update if exist, otherwise insert.
my primary key "id". don't want use unique index "query" column.
what want insert/update :
insert my_table (query, keyword) values ('home', 'home-link'); insert my_table (query, keyword) values ('contact', 'contact-link');
my table:
-------------------------------- id | query | keyword | -------------------------------- 1 | home | home-link | -------------------------------- 2 | contact | contact-link | --------------------------------
i have tried gives me sql syntax error #1064:
if exists(select query my_table query='home') update my_table set query='home' , keyword='home-link' query='home' else insert my_table (query, keyword) values ('home', 'home-link') end if if exists(select query my_table query='contact') update my_table set query='contact' , keyword='contact-link' query='contact' else insert my_table (query, keyword) values ('contact', 'contact-link') end if
assuming have unique index on query
column in table, can upsert this:
insert my_table (query, keyword) values ('home', 'home-link') on duplicate key update keyword = values(keyword)
more info: http://mechanics.flite.com/blog/2013/09/30/how-to-do-an-upsert-in-mysql/
Comments
Post a Comment