?
Solved

update from select mysql

Posted on 2015-01-12
7
Medium Priority
?
376 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 14

Accepted Solution

by:
Mark Bullock earned 2000 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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 14

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

765 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