Jump to content

Updating a table with contents of another table


Recommended Posts

I have a couple tables with the following (trimmed) schemas

 

reps

-----------

id (PK)

name

state (contains 2 char state abbr)

 

states

----------

id (PK)

name

abbr (contains 2 char state abbr)

 

 

What I'd like to do, is to read the reps.state value, and replace it with the state.id value...

something like this:

UPDATE reps
SET reps.state = (
    SELECT states.id
    FROM states
    WHERE states.abbr = reps.state
) ;

I know that query won't work, but that should give you the idea of what I'm looking for.

 

Any ideas?  I could loop through with PHP, but I was looking for a more elegant MySQL only method.

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.