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
  • 2
LVL 59

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 59

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Index not getting used in mysql query 2 41
mysql sql statement - SQL INSERT INTO SELECT 11 59
mysql Encryption with PHP 8 94
MySQL ERROR 1045 (28000) 2 61
Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (…
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

937 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

9 Experts available now in Live!

Get 1:1 Help Now