简体   繁体   中英

Combine columns from different tables and assign to new table

I am working on a small project, where I have 2 different tables, which shows the inventory count of users A and B. Consider the table names to be the same. In each table, I have a column called count, which indicates the respective inventory count of each user. So:

A.item | A.count     B.item | B.count     C.item | c.count

   XYZ | 25             XYZ | 31             XYZ | 0

I have a third table C, which has an empty count at the moment. As you can see, the item name (or an id) is common for all 3 tables. What I want to do, is to add the count for users A and B, and then assign it to C. So, what I think I should be doing is;

UPDATE C set count = A.count + B.count WHERE A.item = B.item

Obviously the above syntax doesn't work. The only thing I've managed to get going so far, is to just show the respective counts from both the table, by using the following code:

SELECT A.count, B.count
FROM A
INNER JOIN B ON A.item = B.id
LIMIT 0 , 30

But with the above code, I don't know how to proceed, so that I can sum the counts from A and B, then assign it to C.count. I tried using a while loop in php, and going through each count row by row - querying over and over, but it takes a long time, and it usually times out, based on the default php timeout setting.

Any help would really be appreciated.

Edit: The above question has been clearly answered by Tim. I'm wondering, how do I modify Tim's code, so that, instead of the count, now I have 2 columns with strings. So:

A.item | A.comment     B.item | B.comment     C.item | c.comment

   XYZ | 25               XYZ | 31               XYZ | 0

How do I modify Tim's code to take the comments from A and B, and add them to C? I tried the following:

UPDATE C c
LEFT JOIN A a
    ON a.item = c.item
LEFT JOIN B b
    ON b.item = c.item
SET c.comment = COALESCE(a.comment, 0) + COALESCE(b.comment, 0)

But it doesn't seem to be this straight forward. I tried looking up the documentation for COALESCE, but i was not able to relate it to this issue I'mm having now, with string. Any help?

Join the three tables together by item, and then update the count of C as being the sum of the other two.

UPDATE C c
LEFT JOIN A a
    ON a.item = c.item
LEFT JOIN B b
    ON b.item = c.item
SET c.count = COALESCE(a.count, 0) + COALESCE(b.count, 0)

I used left join only in the above update, in case a given item in C have only a count in A but not B , or vice-versa. If you instead want the count to be assigned only if both A and B have counts, then replace the left joins with inner joins.

Actually, following on from Tim's code, this code solves my updated question - ie joining strings.

UPDATE C c
LEFT JOIN A a
    ON a.item = c.item
LEFT JOIN B b
    ON b.item = c.item
SET c.comment = CONCAT(c.comment, a.comment, b.comment);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM