Solved

Mysql insert  or update depending on if record exists

Posted on 2014-12-01
6
265 Views
Last Modified: 2014-12-01
I need to update a Table Temp_MyTools

using this criteria
Create Table TempMyUpdate(Select MAX(UploadDate) as UploadDate,SerialNumber, FROM Inventory_SerializedAssets Where LocationID in(1));
update Temp_MyTools mt
Join Temp_MyUpdate as mu on mu.serialnumber = mt.serialnumber
   set mt.FUploadTime = mu.UploadDate


if there is nothing to update then Insert

/////insert
INSERT INTO Temp_MyTools(Select ToolType,SerialNumber,MAX(UploadDate) as FUploadTime, Cast('2012:01:01 12:00:00 AM'as DateTime) AS 'HUploadTime',  LocationID as DefaultLocationIndex, 1 as Qty FROM Inventory_SerializedAssets Where LocationID in(1) GROUP BY SerialNumber);

the tricky part is that this is part of a large query so it is not just by itself how would I do this?
0
Comment
Question by:r3nder
  • 3
  • 3
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 40473844
you want to use the insert with on duplicate key update syntax:
http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
0
 
LVL 6

Author Comment

by:r3nder
ID: 40473851
would this work?
Create Table Temp_Update(Select MAX(UploadDate) as UploadDate,SerialNumber, FROM Inventory_SerializedAssets Where LocationID in(" + locationIndex + ")); 

             IF(Select * From Temp_MyTools Where SerialNumber in(Select SerialNumber from Temp_Update )IS NULL THEN  
                         INSERT INTO Temp_MyTools(Select ToolType,SerialNumber,MAX(UploadDate) as FUploadTime, Cast('2012:01:01 12:00:00 AM'as DateTime) AS 'HUploadTime',  LocationID as DefaultLocationIndex, 1 as Qty FROM Inventory_SerializedAssets Where LocationID in('" + locationIndex + "') GROUP BY SerialNumber);  
             ELSE UPDATE Temp_MyTools mt 
             Join Temp_MyUpdate as mu on mu.serialnumber = mt.serialnumber 
             set mt.FUploadTime = mu.UploadDate 

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40473858
it will likely not work, as you will have some value on serialnumber that already are in the table, and others are not.
either then you run this per single serial number, or use the syntax I proposed
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 6

Author Comment

by:r3nder
ID: 40473880
like this
INSERT INTO Temp_MyTools(Select ToolType,SerialNumber,MAX(UploadDate) as FUploadTime, Cast('2012:01:01 12:00:00 AM'as DateTime) AS 'HUploadTime',  LocationID as DefaultLocationIndex, 1 as Qty FROM Inventory_SerializedAssets Where LocationID in('" + locationIndex + "') GROUP BY SerialNumber)  ON DUPLICATE KEY UPDATE FUploadTime = VALUES(FUploadTime);

Open in new window

0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40473914
that looks good :)
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40474089
Worked like a champ ! Thanks Guy
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now