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 

see in action here


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -