Solved

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

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Scripts of Schema/Data with "WHERE" clause 6 62
Add different cell to otherwise similiar row 4 39
SQL Server 2012 r2 - Varible Table 3 24
VB.net and sql server 4 35
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

773 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