简体   繁体   English

合并来自不同表的列并分配给新表

[英]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. 我正在做一个小项目,其中有2个不同的表,该表显示用户A和B的库存数量。请考虑表名相同。 In each table, I have a column called count, which indicates the respective inventory count of each user. 在每个表中,我都有一个称为count的列,它指示每个用户的各自库存计数。 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. 我有第三个表C,该表目前为空。 As you can see, the item name (or an id) is common for all 3 tables. 如您所见,项目名称(或ID)在所有3个表中都是通用的。 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; 我想做的是将用户A和B的计数相加,然后将其分配给C。因此,我认为我应该做的是:

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. 但是使用上面的代码,我不知道如何进行操作,因此我可以对A和B中的计数求和,然后将其分配给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. 我尝试在php中使用while循环,并逐行遍历每个计数-一遍又一遍地查询,但要花很长时间,并且通常会根据默认的php超时设置而超时。

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. 我想知道如何修改Tim的代码,以便现在有2列包含字符串,而不是计数。 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? 我如何修改Tim的代码以从A和B中获取注释,并将其添加到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. 我尝试查找COALESCE的文档,但无法将其与我现在遇到的带有字符串的此问题相关联。 Any help? 有什么帮助吗?

Join the three tables together by item, and then update the count of C as being the sum of the other two. 将三个表按项目连接在一起,然后将C的计数更新为其他两个表的和。

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. 我用左连接仅在上述更新,如果在给定的项目C具有只在一个计数A但不B ,或反之亦然。 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. 如果您只希望仅在AB都有计数时才分配计数,则将左联接替换为内部联接。

Actually, following on from Tim's code, this code solves my updated question - ie joining strings. 实际上,根据Tim的代码,此代码解决了我更新的问题-即连接字符串。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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