Solved

Mysql insert  or update depending on if record exists

Posted on 2014-12-01
6
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 143

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 143

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 143

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

717 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