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?

 

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.