Solved

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

Posted on 2009-07-12
2
158 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now