Updating fields in one table via querying another table

Posted on 2011-02-13
Last Modified: 2012-05-11
hi guys i am trying to populate how many products are available within a certain category in Mysql database but i am unsure of the sql query to run. the 2 table examples are below.

Table 1 with fields id/category/product. field "id" is an auto increment field and can be ignored.
the layout is referenced below

i need to somehow get a total amount of products that are within each category i.e in the example above there would be a total products of 4 and they are within category 1 and 1 item within category 4

Table 2 has fields named id/category/product. the category names only appear once so go from 1 to 20 for example but i must update the field within this table named "product" and put the amount of products found within table 1 into each category

So if the query ran i would expect an update of field "product" in table 2 to then change to the below

hope this is easy enough to follow and thank you in advance for your help
Question by:TG-Steve
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 60

Expert Comment

by:Kevin Cross
ID: 34883271

Yes, you can use an update with a join like so in MySQL:

UPDATE Table2 t2, (SELECT category, COUNT(product) `cnt` FROM Table1) t1
SET t2.product = t1.cnt
WHERE t2.category = t1.category

Open in new window

LVL 60

Accepted Solution

Kevin Cross earned 250 total points
ID: 34883273
Sorry, forgot the GROUP BY:
UPDATE Table2 t2, (SELECT category, COUNT(product) `cnt` FROM Table1 GROUP BY category) t1
SET t2.product = t1.cnt
WHERE t2.category = t1.category

Open in new window


Author Closing Comment

ID: 34925656
Tested today and works perfect thankyou

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I have been using r1soft Continuous Data Protection ( for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

636 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