MySQL Query sets values to 0 with 'AND' clause - mysql

I have a MySQL query that is meant to update two columns by increments of 1, or create them if they do not exist.
My current table has four columns: id, username, playtime, and deathtime.
As it stands now, this is the query I use:
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1
AND `deathtime`=`deathtime`+1
(The username is dynamic in application, but one is provided for the example).
My issue, however, is that on a duplicate key it doesn't increment the values, rather it sets both values to 0. Through numerous tests of tweaking the query, I have found that it is the AND clause that causes the query to "reset" the values. For instance, this query will increment the playtime column correctly:
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1
And if you swap "playtime" with "deathtime", then it increments the other column correctly. Why is this?

Use a comma to delimit statements here.
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1, `deathtime`=`deathtime`+1
The example can be seen in the documentation here: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

Try this.. a comma, instead of AND
INSERT INTO `playTime` (`username`, `playtime`, `deathtime`)
VALUES ('Rogue', 1, 1)
ON DUPLICATE KEY UPDATE `playtime`=`playtime`+1 ,
`deathtime`=`deathtime`+1

Related

Insert or update (if row with key exists) a pack of rows

My aim is to upload csv data to myisam table: if any row in myisam table has primary key duplicate, update some fields, otherwise - insert new row. For simple inserting i was reading csv line by line, and after every 5000th line i was making an INSERT query like
INSERT INTO table VALUES (), (), (), () ..5k times..,()
For my current aim, i should use query like
INSERT INTO table VALUES (), (), ()...,() ON DUPLICATE KEY UPDATE field1=value1, field2=value2, ...
but it will use value1 and value2 for each row to update. I want it to update with respective values. Hope you understand all written before...
You can use references to VALUES to do this:
INSERT INTO `user`(`username`, `password`) VALUES ('ersks','Nepal'), ('segesg','esgessgs'), ('segsegss','segsege')
ON DUPLICATE KEY UPDATE `username`='master',`password`=VALUES(b);
See docs here https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

MYSQL ON DUPLICATE KEY UPDATE and IGNORE in one query

Ik execute a query that inserts some values in de table, if a combination of ID, Year, rownum (unique index) exists that i do a regular ON DUPLICATE KEY UPDATE and so the row is updated. The query looks like this:
INSERT INTO data_advertenties_prijzen (`ID`, `year`, `rownum`, `status_prijs`,
`datum_dag`, `timestamp_dag`)
VALUES (100,2014,1,1,'2014-01-01',1388534400),
(100,2014,2,1,'2014-07-16',1405468800),
(100,2014,3,1,'2014-07-26',1406332800)
ON DUPLICATE KEY UPDATE
status_prijs = VALUES(status_prijs),
datum_dag = VALUES(datum_dag),
timestamp_dag = VALUES(timestamp_dag)
Nothing difficults there, but….
I also want to do a ON DUPLICATE IGNORE for 1 value in the same query. I Also want to insert one row for 2015. For example: (100,2015,1,1,'2015-01-01',1405468800)…
If there is already a row with ID=100, Year=2015 And rownum=1 the insert of that row must be ignored.
How to do that?
You could change the values conditionally in the ON DUPLICATE clause.
I did this experiment to make sure it works:
INSERT INTO data_advertenties_prijzen VALUES (100, 2014, 1, 7, now(), 1406332800)
ON DUPLICATE KEY UPDATE
status_prijs = IF((id,year,rownum)=(100,2015,1), status_prijs, VALUES(status_prijs)),
datum_dag = IF((id,year,rownum)=(100,2015,1), datum_dag, VALUES(datum_dag)),
timestamp_dag = IF((id,year,rownum)=(100,2015,1), timestamp_dag, VALUES(timestamp_dag));
So if I try to insert a specific trio of id/year/rownum, it just uses the existing value, else if it's some other id/year/rownum, it uses the VALUES I specify in the INSERT.
Unfortunately, you must repeat the expression for each column you want to update.

Insert to table or update if exists using MYSQL database

I have mysql database. I need to update country list on my table. there is some country in my table. I need to check that country if not exist and insert to the table. I'm used following sql script. But this is not working. when execute this code it will duplicate the record.
MySQL Query:
INSERT INTO `moneyexpressstore`.`countries` (`Name`, `Code`, `CurrencyId`) VALUES
('Australia', 'au', NULL) ON DUPLICATE KEY UPDATE Name=VALUES(Name)
thanks,
First make sure your database does not have duplicate records on column countries, then execute this
CREATE UNIQUE INDEX countriesindex ON countries (Name(50))
50 is the amount of characters that the index will search for "unique" values. For example if that number was 3, then these two different string would be considered the same, and a 1062 Duplicate Entry error would occur abcHELLO=abcWORLD and in your case it would force the UPDATE instead of INSERT.
If you get a 1062 error, that means you have duplicates in your db so find them remove them and try again.
After this your query will execute just fine and will update instead of duplicate on "Name"
Have a look in the documentation of mysql https://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Try this:
insert into `moneyexpressstore`.`countries` (id, `Name`, `Code`, `CurrencyId`) values(NULL,'Australia', 'au', NULL) on duplicate key update name=values(name)
Please refer this link:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

MySQL: mass insert new records or increment existing

I need to make something like this:
INSERT `stats` (`id`,`clicks`) VALUES
(1, clicks+7),
(2, clicks+3),
....
ON DUPLICATE KEY UPDATE `clicks`=VALUES(clicks)
in other words, when table has no record with pk id - it inserted and clicks gets 7 (or 3). When record with PK is present, then old value of click should be increased by 7 (or 3). As you can see, increment value for each row is different. Current query always overwrites old value. Please help to modify this query.
The VALUES have to be literal values, not references to columns:
INSERT INTO `stats` (`id`,`clicks`) VALUES
(1, 7),
(2, 3)
ON DUPLICATE KEY UPDATE `clicks`=`clicks` + VALUES(`clicks`)

MySql: REPLACE INTO with number=number+1

I would like do something like this in one only query.
REPLACE INTO table ( id, number ) VALUES ( 'test', number=number+5 )
What I want is (the first time!) insert the row and set the number 5.
the other times (if already exist) add 5 at the number.
Is it possible? I can't find nothing on line.
just be sure that ID is unique. Use INSERT ... ON DUPLICATE KEY UPDATE Syntax
INSERT INTO tableName (id, number)
VALUES ('test', 5)
ON DUPLICATE KEY UPDATE
number = number + 5
Assuming that id is a proper key (e.g. primary key):
INSERT INTO `table` (id, number)
VALUES ('test', 5)
ON DUPLICATE KEY UPDATE number=number+VALUES(number)