Solved

update from select mysql

Posted on 2015-01-12
7
352 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Designer 19 44
Need help how to find where my error is in UFD 6 30
performance query 4 24
awk and Pythagoras? 5 8
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 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