Solved

update from select mysql

Posted on 2015-01-12
7
334 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
Comment Utility
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
Comment Utility
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
Comment Utility
@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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 6

Author Comment

by:r3nder
Comment Utility
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
Comment Utility
Add a WHERE clause with the criteria to the UPDATE to avoid setting to zero.
0
 
LVL 6

Author Comment

by:r3nder
Comment Utility
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
Comment Utility
Thank  you sir!
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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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

8 Experts available now in Live!

Get 1:1 Help Now