Solved

Updating fields in one table via querying another table

Posted on 2011-02-13
3
297 Views
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
1/1/1
2/1/2
3/1/3
4/1/4
5/4/5

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
1/pens/0
2/rulers/0
3/mats/0
4/inks/0

So if the query ran i would expect an update of field "product" in table 2 to then change to the below
1/pens/4
2/rules/0
3/mats/0
4/inks/1

hope this is easy enough to follow and thank you in advance for your help
0
Comment
Question by:TG-Steve
  • 2
3 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
TG-Steve,

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


Regards,
Kevin
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
Comment Utility
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

0
 

Author Closing Comment

by:TG-Steve
Comment Utility
Tested today and works perfect thankyou
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) 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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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.

771 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

10 Experts available now in Live!

Get 1:1 Help Now