Solved

Updating fields in one table via querying another table

Posted on 2011-02-13
3
301 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
ID: 34883271
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
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

0
 

Author Closing Comment

by:TG-Steve
ID: 34925656
Tested today and works perfect thankyou
0

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
PHP loop not working 4 62
PHP & MySql - Turn Select Query Results into a Glyphicon 3 31
when to use sequences in mysql 4 29
Complex SQL statement in VB.NET 7 15
A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Creating and Managing Databases with phpMyAdmin in cPanel.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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