Updating column value to the ID associated with it in another table.

Ok working on someone else's code I've run into the following problem with their photo gallery. There is a table to store the image, image path, gallery, etc... and a table to store the gallery name, a short name (for the url) and an id for the gallery. However the image table is story the short name for the gallery instead of the id. Example data:

|id_image|img_filename|img_ext|img_path|  img_gal  | img_date |img_photographer|
|   1    | dsc_004    | jpg   | ...    | golf_2011 |2011-06-10| My Name        |
|   2    | dsc_027    | jpg   | ...    | golf_2011 |2011-06-10| My Name        |
|   3    | dsc_523    | jpg   | ...    | dtco_2011 |2011-09-20| Someone's Name |
|   4    | dsc_544    | jpg   | ...    | dtco_2011 |2011-09-20| My Name        |

Open in new window

|id_gal|  gal_name         | gal_short |
|   1  | 2011 Golf Outting | golf_2011 |
|   2  | 2011 Celebrity ...| dtco_2011 |

Open in new window

What I'm trying to do is create a query, that will update the gallery_images table, img_gal column to instead have the id of the gallery as listen in the gallery_groups table.
LVL 10
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DerokorianAuthor Commented:
Nevermind - I found out you can subquery within an update query. All my data is the way I want it now, just need to fix some code that relied on poor design. My query looks like:
UPDATE gallery_images SET
   img_gal = (SELECT id_gal FROM gallery_groups WHERE gal_short = img_gal)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DerokorianAuthor Commented:
I was able to find the answer on my own. Sorry to waste the experts' time!
Guy Hengel [angelIII / a3]Billing EngineerCommented:
though the question is closed, you can read up this article for update with join:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.