开发者

Merge computed data from two tables back into one of them

开发者 https://www.devze.com 2023-01-03 00:10 出处:网络
I have the following situation (as a reduced example). Two tables, Measures1 and Measures2, each of which store an ID, a Weight in grams, and optionally a Volume in fluid onces. (In reality, Measures1

I have the following situation (as a reduced example). Two tables, Measures1 and Measures2, each of which store an ID, a Weight in grams, and optionally a Volume in fluid onces. (In reality, Measures1 has a good deal of other data that is irrelevant here)

Contents of Measures1:

+----+----------+--------+
| ID | Weight   | Volume |
+----+----------+--------+
|  1 | 100.0000 |   NULL |
|  2 | 200.0000 |   NULL |
|  3 | 150.0000 |   NULL |
|  4 | 325.0000 |   NULL |
+----+----------+--------+

Contents of Measures2:

+----+----------+----------+
| ID | Weight   | Volume   |
+----+----------+----------+
|  1 |  75.0000 |  10.0000 |
|  2 | 400.0000 |  64.0000 |
|  3 | 100.0000 |  22.0000 |
|  4 | 500.0000 | 100.0000 |
+----+----------+----------+

These tables describe equivalent weights and volumes of a substance. E.g. 10 fluid ounces of substance 1 weighs 75 grams. The IDs are related: ID 1 in Measures1 is the same substance as ID 1 in Measures2.

What I want to do is fill in the NULL volumes in Measures1 using the information in Measures2, but keeping the weights from Measures1 (then, ultimately, I can drop the Measures2 table, as it will be redundant). For the sake of simplicity, assume that all volumes in Measures1 are NULL and all volumes in Measures2 are not.

I can compute the volumes I want to fill in with the following query:

SELECT Measures1.ID, Measures1.Weight, 
       (Measures2.Volume * (Measures1.Weight / Measures2.Weight)) 
       AS DesiredVolume 
FROM Measures1 JOIN Measures2 ON Measures1.ID = Measures2.ID;

Producing:

+----+----------+-----------------+
| ID | Weight   | DesiredVolume   |
+----+----------+-----------------+
|  4 | 325.0000 | 65.000000000000 |
|  3 | 150.0000 | 33.000000000000 |
|  2 | 200.0000 | 32.000000000000 |
|  1 | 100.0000 | 13.333333333333 |
+----+----------+-----------------+

But I am at a loss for how to actually insert these computed values into the Measures1 table.

Preferably, I would like to be able to do it with a single query, rather than writing a script or stored procedure that iterates through every ID in Measures1. But even then I am worried that this might not be possible because the MySQL documentation says tha开发者_如何学Got you can't use a table in an UPDATE query and a SELECT subquery at the same time, and I think any solution would need to do that.

I know that one workaround might be to create a new table with the results of the above query (also selecting all of the other non-Volume fields in Measures1) and then drop both tables and replace Measures1 with the newly-created table, but I was wondering if there was any better way to do it that I am missing.


UPDATE Measures1
SET Volume = (Measures2.Volume * (Measures1.Weight / Measures2.Weight)) 
FROM Measures1 JOIN Measures2
ON Measures1.ID = Measures2.ID;
0

精彩评论

暂无评论...
验证码 换一张
取 消