Nixi Nixi - 1 year ago 133
SQL Question

c# mysql insert query, if record exists then update

I am trying to insert the new records in table, I have two columns

| Attribute | Datatype |
| user_id | varchar(25)|
| count | int |

Query :

INSERT into user_records Values("nix_1", 0)

but if the user already exists, then it should increase count by 1

Answer Source

Why not use the INSERT ... ON DUPLICATE KEY syntax?

INSERT INTO user_records (user_id, count) VALUES ('nix_1', 0)
ON DUPLICATE KEY UPDATE count = count + 1;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download