Solved

Mysql insert  or update depending on if record exists

Posted on 2014-12-01
6
262 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
Comment Utility
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
Comment Utility
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]
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Author Comment

by:r3nder
Comment Utility
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]
Comment Utility
that looks good :)
0
 
LVL 6

Author Closing Comment

by:r3nder
Comment Utility
Worked like a champ ! Thanks Guy
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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.

763 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

10 Experts available now in Live!

Get 1:1 Help Now