Jump to content

New set of eyes on my Query - UPDATE, SET 2 Tables


OldWest

Recommended Posts

Can anyone just have a look at tell me if my query and tell me if it seems optimal and optimized for what I am trying to do.

 

I've got 2 tables:

 

locations_all

sys_states

 

I need to update a blank field locations_all.state_id to sys_states.id where locations_all.city_state equals sys_states.stateName

 

Here is my current query:

 

UPDATE locations_all AS a JOIN sys_states AS b ON a.city_state = b.stateName SET a.state_id = b.id

 

Does it seem efficient this way?

 

Link to comment
Share on other sites

it is good... that is the way to do it.... just one observation based on your description:

....

I need to update a blank field .....

 

you want to do the update ONLY if the locations_all.state_id is blank (empty, null or 0.. depending of the data type)... or you want to update it always? 

 

If the first case you need a condition (WHERE clause) in that UPDATE  ... otherwise you are good to go

Link to comment
Share on other sites

Hey mikosiko,

 

Thanks for the great feedback :) I see now how it could have been a disaster had I needed to keep existing data in the locations_all.state_id field!

 

All locations_all.state_id fields where blank. And from my manual review and check everything seems to have been updated accurately.

 

It took a lot of guts for me to run this even on test tables!

 

I'll call this one SOLVED!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.