?
Solved

MySQL UPDATE

Posted on 2011-05-12
10
Medium Priority
?
329 Views
Last Modified: 2012-05-11
I'm looking for a single statement to populate a new StatusID field with the corresponding index from the StatusNames table, where currently there is just enumerated text in the Status field

something like:

UPDATE MyTable SET MyTable.StatusID=StatusNames.ID WHERE MyTable.Status=StatusNames.Name

such that it would populate all  StatusID entries in one single statement.

how to accomplish this cross-table lookup?

thanks
0
Comment
Question by:willsherwood
10 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 1000 total points
ID: 35747825
UPDATE t
SET  t.StatusID= sn.ID
FROM  MyTable t INNER JOIN StatusNames sn ON sn.Name = t.Status
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35747841
enumerated text in the Status field

oops... i missed that.
0
 
LVL 41

Expert Comment

by:Sharath
ID: 35747933
can you provide some sample and what are you trying to achieve?
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Author Comment

by:willsherwood
ID: 35751247
StatusNames
ID  Name
1    Active
2    Expired

MyTable
ID   StatusID    Status
10       *1            Active
11       *2            Expired

I want the * to be filled in with 1 and 2 as shown.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35751612
And you've tried the JOIN and that doesn't work?
0
 

Author Comment

by:willsherwood
ID: 35753656
not yet - i thought the other comments intended to retract the suggestion, i'll work on it soon. thanks
0
 

Author Comment

by:willsherwood
ID: 35754840
Please help with syntax error - i've applied to a specific example (spelling out all table names, thus no alias):

UPDATE PropertyListings
SET PropertyListings.TownshipID= Townships.ID
FROM  PropertyListings INNER JOIN  Townships ON Townships.Name = PropertyListings.Township
0
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 1000 total points
ID: 35755201
If you are using MySQL, the update syntax is as follows:

UPDATE PropertyListings tto, Townships tfr
SET tto.TownshipID= tfr.ID
WHERE tfr.Name = tto.Township
;

Open in new window


This should work also with ANSI style JOIN syntax; the gist is the JOIN goes first in MySQL.
UPDATE PropertyListings tto
JOIN Townships tfr ON tfr.Name = tto.Township
SET tto.TownshipID= tfr.ID
;

Open in new window

0
 

Author Closing Comment

by:willsherwood
ID: 35755332
thanks all!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35755911
If you are using MySQL

Oops my bad.  I had *MS* SQL on the brain ;-)
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 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