Convert UPDATE into INSERT INTO ON DUPLICATE KEY UPDATE statement - mysql

I have this UPDATE MySQL statement which works fine.
UPDATE table1
Inner Join table2
ON table2.id = table1.gw_id
SET table1.field1=1, table1.field2=2
WHERE table1.nick_no=4 AND table2.addr=123
I would like to convert this UPDATE statement such that it can add a new row if a row with the same table1_nick_no is not found. I believe using INSERT INTO ON DUPLICATE KEY UPDATE is the right way to go. However, I tried for a long time but failed. Adding Inner join and where clause to INSERT INTO ON DUPLICATE KEY UPDATE
How should I convert this UPDATE statement into the corresponding INSERT INTO ON DUPLICATE KEY UPDATE statement?

What you are looking for is a MERGE or an UPSERT (short for UPDATE/INSERT). In mySQL you can achieve an UPSET as described in the following link
http://mechanics.flite.com/blog/2013/09/30/how-to-do-an-upsert-in-mysql/

Related

how to give where condtion after on duplication key in trigger

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?

MySQL insert on duplicate key; delete?

Is there a way of removing record on duplicate key in MySQL?
Say we have a record in the database with the specific primary key and we try to add another one with the same key - ON DUPLICATE KEY UPDATE would simply update the record, but is there an option to remove record if already exists? It is for simple in/out functionality on click of a button.
It's a work-around, but it works:
Create a new column and call it do_delete, or whatever, making it a tiny-int. Then do On Duplicate Key Update do_delete = 1;
Depending on your MySQL version/connection, you can execute multiple queries in the same statement. However, if not, just run a separate query immediately afterwords. Either way, the next query would just be: Delete From [table] Where do_delete = 1;. This way, if its a new entry, it will not delete anything. If it was not a new entry, it will then mark it for deletion then you can delete it.
Use REPLACE INTO:
replace into some_table
select somecolumn from othertable
will either insert new data or if thr same data exist will delete the data and insert the new one
The nearest possible solution for the same is REPLACE statement. Here is the documentation for REPLACE.
A similar question was asked on MySQL Forums and the recommended(and only) answer was to use REPLACE.
to be more clear with mySql:
values can be from same table:
replace into table1 (column1,column2) select (val1,val2) from table1
or
values can be from another table:
replace into table1 (column1,column2) select (val1,val2) from table2
or
values can be from any table with condition:
replace into table1 (column1,column2) select (val1,val2) from table1 where <br>column3=val3 and column4=val4 ...
or
also remember values can be static with table name for namesake:
replace into table1 (column1,column2) select (123,"xyz") from table1
no error will be thrown even if the update results in duplicate entry, as it will be replaced.
(remember) only autoincrement value will be increased;
and
if you have column with data-type "TIMESTAMP" with "on update CURRENT_TIMESTAMP", it will have no effect;
Yes of course there is a solutions in MySQL for your problem.
If you want to delete or skip the new inserting record if there already a duplicate record exists in the key column of the table, you can use IGNORE like this:
insert ignore into mytbl(id,name) values(6,'ron'),(7,'son');
Here id column is primary key in the table mytbl. This will insert multiple values in the table by deleting or skipping the new duplicate records.
Hope this will fulfill your requirement.

Mysql + Update to table (same time) with Join

I believe this is possible but I haven't done it before.
I need to update 2 tables and I want to use one MySQL command. I also have a join field.
UPDATE static_site_articles SET domainname='a',language='b',url='domain',toptext='c',bottomtext='d' WHERE id='10;
UPDATE static_site_articles_meta SET url='domain',title='e',description='f',keywords='g';
They join with the url='domain'.
How can I construct this into a single UPDATE command line?
A push in the right direction would be greatly appreciated.
thx
http://dev.mysql.com/doc/refman/5.1/de/insert-on-duplicate.html
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;

how to insert without duplicate from one table into another

I tried to insert from one table into another and im having with the redundancy..
I came up with a query but every time when i execute it, It cannot deals with duplicate.
here's my query...
INSERT INTO balik ( balik_date, balik_time, balik_cardID, balik_status,balik_type)
select current_date(), '00:00:00', L_CardID, 'BELUM BALIK', L_Type
FROM logdetail t1
LEFT JOIN balik t2 ON (t1.L_CardID = t2.balik_cardID)
WHERE t1.L_Type = 'IN'
any help will be greatly appreciated
Use INSERT IGNORE instant of INSERT.
Use INSERT IGNORE rather than INSERT. If a record doesn't duplicate an
existing record, MySQL inserts it as usual. If the record is a
duplicate, the IGNORE keyword tells MySQL to discard it silently
without generating an error.
OR
Check row count for unique field. If row exist don't insert or update.
OR
Use REPLACE rather than INSERT. If the record is new, it's inserted
just as with INSERT. If it's a duplicate, the new record replaces the
old one:
Source for definitions MySQL Handling Duplicates

UPDATE record if present; else INSERT

I want to update a record which may or may not be present in a table. If it is not present in the database then it will be inserted.
To prevent from select I am using UPDATE statement first and checking affected_rows > 0 if not then I am inserting this record into the table.
I was wondering if there is a better way to do this?
You could use INSERT ... ON DUPLICATE KEY UPDATE syntax:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html
The difference between this and REPLACE (Femaref's answer) is that REPLACE will delete the old row and then insert a new row if a key is duplicated, while this will update the existing row.
Use Replace instead of Insert.
http://dev.mysql.com/doc/refman/5.0/en/replace.html