Solved

update from select mysql

Posted on 2015-01-12
7
370 Views
Last Modified: 2015-01-13
I am trying to update a table and set the quantity from a temp table
UPDATE `Inventory_Parts` SET `Quantity`= (SELECT Qty FROM Temp_Counts tc
JOIN Inventory_Parts as ip ON ip.InventoryListID = tc.UID and LocationID = 0)

Open in new window

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
  • 4
  • 2
7 Comments
 
LVL 13

Accepted Solution

by:
Mark Bullock earned 500 total points
ID: 40545706
Remove the apostrophes around Inventory_Parts and Quantity.

I would not do a join in the subquery. Instead use WHERE InventoryListID = tc.UID
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40545895
So this is all fine and dandy if the subquery returns only one row.  Anything larger than that and the code will throw an error.   So, added SUM..
UPDATE Inventory_Parts
SET Quantity= (
	SELECT SUM(Qty) 
	FROM Temp_Counts tc
	   JOIN Inventory_Parts ip ON ip.InventoryListID = tc.UID and LocationID = 0)

Open in new window


Also, SQL Server does not require the single quote marks ', not sure if MySQL uses that.
0
 
LVL 6

Author Comment

by:r3nder
ID: 40546610
@Jim MySQL uses the single quote but in any event the error I am getting is "You can't specify target table 'Inventory_Parts' for update in FROM clause" on both your query and mine
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 6

Author Comment

by:r3nder
ID: 40546687
This seems to have updated everything to zero if it didn't meet the criteria
UPDATE Inventory_Parts
SET Quantity= (
	SELECT Qty 
	FROM Temp_Counts tc
	   WHERE InventoryListID = tc.UID  and LocationID = 0)

Open in new window

0
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40546759
Add a WHERE clause with the criteria to the UPDATE to avoid setting to zero.
0
 
LVL 6

Author Comment

by:r3nder
ID: 40546776
Like this
UPDATE Inventory_Parts
SET Quantity= (
	SELECT Qty 
	FROM Temp_Counts tc
	   WHERE InventoryListID = tc.UID) WHERE LocationID = 0

Open in new window

0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40546810
Thank  you sir!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

690 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