I am working on the database for a Wordpress Multisite install and need to copy multiple fields (but not all) from one table into another table, replace existing content and/or add row if they don't exist.
There are similar answers elsewhere but I can't seem to figure this out.
tables are sourcetable and targettable
columns are option_name and option_value
basically, if option_name field = condition1
is present in targettable copy option_value field from sourcetable and replace targettable option_value field, if not present then add it
would like to update/replace multiple fields with one query
REPLACE or UPDATE option_value_field in targettable with data
from sourcetable value_field
where name_field equals "condition1"
REPLACE or UPDATE option_value_field in targettable with data
from sourcetable value_field
where name_field equals "condition2"
REPLACE or UPDATE option_value_field in targettable with data
from sourcetable value_field
where name_field equals "condition3" (add if does not exist)
thanks
Make option_name field unique, and use INSERT...ON DUPLICATE KEY UPDATE statement to insert/replace values in targettable
-
INSERT INTO targettable
SELECT * FROM sourcetable
ON DUPLICATE KEY UPDATE option_value = VALUES(option_value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.