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
Related
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
I would like to do something like this (the updated value should be different than the inserted value):
"INSERT INTO notification_chat_counts (uid,group_id,count)
VALUES
(",$uid,",",$groupId,",1)
ON DUPLICATE KEY UPDATE
(count = count +1)"
Of course it is possible. Even the MySQL manual has example of inserting different value that the values which would got updated:
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;
For your example, the correct query syntax would be:
$query = "INSERT INTO notification_chat_counts (uid,group_id,count)
VALUES (",$uid,",",$groupId,",1)
ON DUPLICATE KEY UPDATE count=count+1"
However for this statement to work you must have an UNIQUE type index defined for this table, so that MySQL can decide if such row already exists in the table or not.
Also remember that inserting values into SQL query like this is dangerous and not recommended. You should use prepared statements for that.
The following code is a trigger I wrote in Navicat for Mysql. The inserting part is working correctly but for update I need to update a specific row quantity based on id. How do I write the where clause in the update sql query.
insert into closingstockt(CLS_BSID,CLS_Qty,CLS_SQty,CLS_CDate)
select BS_ID as CLS_BSID,BS_Qty as CLS_Qty,BSS_Qty as CLS_SQty,curdate()
from barstockt
where CLS_BSID=BS_ID
on duplicate key update CLS_Qty=BS_Qty,CLS_SQty=BSS_Qty
ON DUPLICATE KEY UPDATE will update the row which already exist in the database. For example:
INSERT INTO table (field,field2) VALUES ('data1','data2')
ON DUPLICATE KEY UPDATE lastupdate=NOW()
EDIT: I suggest to use IF()
INSERT ... ON DUPLICATE KEY UPDATE with WHERE?
I have a query like so:
INSERT INTO table1 (field1,field2) VALUES ('$value1','$value2') ON DUPLICATE KEY UPDATE field1 = '$value1'
I then want to get the last insert id if it does the insert, how can I do this? If the query ends up doing an update I dont want the last insert id. Is there a way to determine if it did an update or a insert?
I guess I should of searched the site before posting. Basically adding this worked:
id=LAST_INSERT_ID(id)
On the on duplicate update. I found that answer here:
Duplicate Key Last Insert ID
According to this MySQL Manual Page:
If a table contains an AUTO_INCREMENT
column and INSERT ... ON DUPLICATE KEY
UPDATE inserts or updates a row, the
LAST_INSERT_ID() function returns the
AUTO_INCREMENT value.
I am trying to implement a rating system where I keep the following two fields in my db table:
rating (the current rating)
num_rates (the number of ratings submitted so far)
UPDATE `mytable`
SET rating=((rating*num_rates)+$theRating)/num_rates, num_rates=num_rates+1
WHERE uniqueCol='$uniqueCol'
the variables are from my PHP code.
So, basically sometimes the row with the uniqueCol does not exist in the DB, so how can I do the above statement if the exists and if it doesn't then do something like this:
INSERT INTO `mytable`
SET rating=$theRating, num_rates=1, uniqueCol=$uniqueCol
Have a look at INSERT ... ON DUPLICATE KEY UPDATE.
It should look something like that:
INSERT INTO mytable (rating, num_rates, uniqueCol)
VALUES ($theRating, 1, $uniqueCol)
ON DUPLICATE KEY UPDATE
rating=((rating*num_rates)+$theRating)/num_rates,
num_rates=num_rates+1;
Make sure to have a UNIQUE index or PRIMARY KEY on your uniqueCol.