Solved

updating a MySQL table with ID numbers based on another table.

Posted on 2009-07-12
2
159 Views
Last Modified: 2012-05-07
I have 2 tables, one called 'products' and one called 'sub_cats'

in the table products there is a column 'sub_cats' which instead of having ID numbers as one would expect, it has the full name of the sub category, found in the sub_cats table.

I need to get the ID number from the sub_cats table and put it in the right fields which corresponds to the right name in the sub_cats table as indicated in the code area below.

Cheers!!

Tim


so its like...
 

products table:
 

ID,  sub_cat, title

1    knife   SK 1

2    fork    SF 1

3    fork    SF 2

4    spoon   SP 1

etc...
 
 

and in the sub_cats table its simply:
 

ID,  sub_cat

1    knife

2    fork

3    spoon
 
 

so I really need the products table to be more like:
 

ID,  sub_cat, title

1    1        SK 1

2    2        SF 1

3    2        SF 2

4    3        SP 1

Open in new window

0
Comment
Question by:timbo007
2 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24834671
this should do:
update products , sub_cats 
  set products.sub_cat = sub_cats.id
where products.sub_cat = sub_cats.sub_cat

Open in new window

0
 

Author Comment

by:timbo007
ID: 24834742
Hah, just as easy as that, I never thought of using join type syntax but it makes perfect sense!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now