Solved

update from select mysql

Posted on 2015-01-12
7
345 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
  • 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 65

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
Viewers will learn how the fundamental information of how to create a table.

920 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

12 Experts available now in Live!

Get 1:1 Help Now