The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Databases
> MySQL Help
|
Many to one update sql
Discuss Many to one update sql in the MySQL Help forum on Dev Shed. Many to one update sql MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 19th, 2012, 04:58 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 12
Time spent in forums: 1 h 52 m 36 sec
Reputation Power: 0
|
|
|
Many to one update sql
Hello:
I am having a problem with the proper SQL to accomplish the following.
I am using MySQL. I have 2 tables, both tables contain 2 columns, column a and column b. In table 1 column a is the key and therefore unique. In table 2 the value in column a can be repeated many times.
What I want to do is update column b in table 1 from column b in table 2 from all rows in table 2 where t1.columna = t2.column a
Thanks in advance
|

December 19th, 2012, 06:27 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
You can do a JOIN in an UPDATE query...
|

December 19th, 2012, 06:52 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 12
Time spent in forums: 1 h 52 m 36 sec
Reputation Power: 0
|
|
|
Join in update
I have tried as
UPDATE table1 t1, table2 t2
SET t1.columnb = t1.columnb + t2.columnb
WHERE t1.columna = t2.columna
This does not work
|

December 19th, 2012, 07:04 PM
|
 |
Lord of the Dance
|
|
|
|
|
Please give more information than "this does not work".
Try with the explicit join syntax:
UPDATE table1 t1 INNER JOIN table2 t2 ON t1.columna = t2.columna
SET t1.columnb = t1.columnb + t2.columnb
|

December 19th, 2012, 08:40 PM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 12
Time spent in forums: 1 h 52 m 36 sec
Reputation Power: 0
|
|
|
More info
I have tried both ways, This is the detai. Table 1 contains customers (customer number,....,arbalance) Table 2 contains invoice information (customer number,...,invoice amount). There can be several invoices in table 2 for any given customer in table 1. In my test I have created a customer file with only 1 customer, say customer 1. The starting arbalance is 0. The test invoice table contains 2 invoices, both for customer 1. (invoice 1, customer 1, amount 200 and invoice 2, customer 1, amount 300). I want to update the custoner tables arbalance with all matching invoices, in the test case there are only 2 and they both match. No mater how I try the update only the 1st invoice updates the customer file. After the update I expect that arbalance will be 500 (starting 0 + 200 + 300). It always equals 200.
|

December 20th, 2012, 07:15 AM
|
 |
Lord of the Dance
|
|
|
|
Just curious why you want to store SUM value, since you always can call SUM() for this tasks?
For example, the below query will give the total invoice amount for each customer (number):
Code:
SELECT customer_number, SUM(invoice_amount)
FROM Invoice
GROUP BY customer_number
Furthermore, when and where did you expect to do the update call?
t1.columnb = t1.columnb + t2.columnb will not take into consideration what had already been added to the sum.
|

December 20th, 2012, 07:34 AM
|
|
Registered User
|
|
Join Date: Jun 2012
Posts: 12
Time spent in forums: 1 h 52 m 36 sec
Reputation Power: 0
|
|
|
Solution
Hello:
I did find a solution very similar to what you suggested.
UPDATE t1
SET columnb = columnb + (SELECT SUM(columnb) FROM
t2 WHERE t1.columna = t2.columna)
Thanks
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|