------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1 | foo | 5 | ------------------------------------- | 2 | bar | 12 | -------------------------------------
user_id: auto increament, user_visits: default 1
INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux');
the above statement will of course insert 3 new records, as the result:
------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1 | foo | 5 | ------------------------------------- | 2 | bar | 12 | ------------------------------------- | 3 | baz | 1 | ------------------------------------- | 4 | bar | 1 | ------------------------------------- | 5 | qux | 1 | -------------------------------------
but what I'm trying to achieve is:
------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1 | foo | 5 | ------------------------------------- | 2 | bar | 13 | ------------------------------------- | 3 | baz | 1 | ------------------------------------- | 4 | qux | 1 | -------------------------------------
so literally,
if field user_name exists, update user_visits, else insert a new record.
is it possible to achieve this with a single insert statement?
The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.
Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.
When you insert a new row into a table if the row causes a duplicate in UNIQUE index or PRIMARY KEY , MySQL will issue an error. However, if you specify the ON DUPLICATE KEY UPDATE option in the INSERT statement, MySQL will update the existing row with the new values instead.
Use the context menu on the same table, to get another script: "Script Table as | SELECT To | New Query Window". This will be a totally standard select list, with all your fields listed out. Copy the whole query and paste it in over the VALUES clause in your first query window. This will give you a complete INSERT ...
Sure there is but it has nothing to do with your insert
statement. You need to add a unique index on the user_name
column:
create unique index user_name_idx on yourtable (user_name);
Then afterward in your code that tracks the count will have to decide whether to do an insert or an update.
You have to create a key for your username field and then use INSERT ON DUPLICATE
query to update the columns values.
For example your query must be,
INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux') ON DUPLICATE KEY UPDATE user_visits=user_visits+1;
For further reference visit http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With