mysql - Update table if two columns exist in table and insert if one of the two columns are different -
i have table computer, has following columns:
computer(computer_id(pk), office_id, computer_name, login, date, time)
i trying create mysql statement update row if office_id
, computer_name
exist , insert new row if not exist. want to still insert new row if office_id
different computer_name
exist , vise versa. if can give me advice on it. appreciate it.
you can use mysql's on duplicate key update
functionality accomplish this.
assuming have unique constraint on (computer_name, office_id)
, following query insert new row in able new pairs of (computer_name, office_id)
, otherwise update existing row:
insert computer (office_id, computer_name, login, `date`, `time`) values (..., ..., ..., ..., ...) on duplicate key update login = values(login), `date` = values(`date`); ^^^^^^^^^^^^^^^^^^^^^ here how specify values update
Comments
Post a Comment